为 C 字符串重载函数模板

Overloading Function Templates for C-strings

以下片段代码是本书 c++ 模板中的示例。我的问题是为什么最后一行中的语句 return max (max(a,b), c) 变成运行时错误。谁能给我一些提示?谢谢!

#include <cstring>
// maximum of two values of any type (call-by-reference)
template<typename T>
T const& max (T const& a, T const& b)
{
return b < a ? a : b;
}
// maximum of two C-strings (call-by-value)
char const* max (char const* a, char const* b)
{
return std::strcmp(b,a) < 0 ? a : b;
}
// maximum of three values of any type (call-by-reference)
template<typename T>
T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c); // error if max(a,b) uses call-by-value
}
int main ()
{
auto m1 = ::max(7, 42, 68); // OK
char const* s1 = "frederic";
char const* s2 = "anica";
char const* s3 = "lucas";
auto m2 = ::max(s1, s2, s3); // run-time ERROR
}

char const* max (char const* a, char const* b) return 是一个 const char * 变量值。然后 T const& max (T const& a, T const& b, T const& c) 创建一个存储该值的临时变量,并且 return 是一个 const& 对它的引用(使用 T = const char *)。当分配 auto m2 = 时,该临时指针不存在。最有可能的是,您稍后打印或检查该值,这会导致某种“运行时错误”。

我想,return 一开始就是对 const char * 的引用。

char const * const& max (char const * const& a, char const * const& b) {
   return std::strcmp(b, a) < 0 ? a : b;
}