这是我的问题:
/**
* Example of the book:
* C++ Templates page 17/18
*/
#include <iostream>
#include <cstring>
#include <string>
// max of two values of any type (call by reference)
template <typename T>
inline T const& max (T const& a, T const& b) {
return a < b ? b : a;
}
// max of two C-strings (call by value)
inline char const* max (char const* a, char const* b) {
// ??? Creates this a new temporary local value that may be returned?
// I can't see where the temporary value is created!
return std::strcmp(a,b) < 0 ? b : a;
}
// max of three values of any type (call by reference)
template <typename T>
inline T const& max (T const& a, T const& b, T const& c) {
return max (max(a,b),c); // warning "error", if max(a,b) uses call-by-value
// warning: reference of temp value will be returned
int main() {
// call by reference
std::cout << ::max(7, 42, 68) << std::endl;
const char* s1 = "Tim";
const char* s2 = "Tom";
const char* s3 = "Toni";
// call the function with call by value
// ??? Is this right?
std::cout << ::max(s1,s2) << std::endl;
std::cout << ::max(s1, s2, s3) << std::endl;
}
C 字符串的函数 max 中的临时局部值在哪里?
函数有两个指针,为什么是按值调用呢?
对不起,我认为这是一个非常愚蠢的问题,但我不明白。
谢谢你。