了解 C++模板错误
#include <iostream>
#include <cstring>
#include <string>
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
inline char const* max (char const* a, char const* b)
{
return std::strcmp(a,b) < 0 ? b : a;
}
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c); // error
}
int main ()
{
::max(7, 42, 68); // OK
const char* s1 = "frederic";
const char* s2 = "anica";
const char* s3 = "lucas";
::max(s1, s2, s3); // ERROR
}
有人可以告诉我为什么这是一个错误吗?
#include <iostream>
#include <cstring>
#include <string>
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
inline char const* max (char const* a, char const* b)
{
return std::strcmp(a,b) < 0 ? b : a;
}
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c); // error
}
int main ()
{
::max(7, 42, 68); // OK
const char* s1 = "frederic";
const char* s2 = "anica";
const char* s3 = "lucas";
::max(s1, s2, s3); // ERROR
}
Could anybody please tell me why this is an error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您说:
max(char*,char*)
返回一个 BY VALUE 指针。然后,您返回对此值的引用。为了实现这项工作,您应该使所有 max() 函数返回值而不是引用,正如我认为在对您上一个问题的回答中所建议的那样,或者使 char* 重载接受并返回对指针的引用。When you say:
max(char*,char*)
returns a pointer BY VALUE. You then return a reference to this value. To make this work, you should make all your max() functions return values rather than references, as I think was suggested in an answer to your previous question, or make the char* overload take and return references to pointers.您正在返回对临时对象的引用。 max 的 char* 重载按值返回,但 3-arg 模板按引用返回。
我不知道你为什么会收到错误。我只在我的编译器(GCC)上收到警告。我想如果你发布了错误文本,有人可以弄清楚。
You're returning a reference to a temporary. The
char*
overload of max returns by value, but the 3-arg template returns by reference.I don't know exactly why you get an error. I only get a warning on my compiler (GCC). I imagine if you posted the error text, though, someone could figure it out.
如果您确实想返回引用,那么您必须修改第二个重载以也返回引用。
If you really want to return a reference, then you have to modify the second overload to return a reference too.
您的示例与此等效,也许您会这样看得更好:
Your example is equivalent to this, maybe you'll see it better this way:
编译时,我得到:
那是因为您返回了对临时对象的引用(即,当函数的调用者可以检查其值时,您的对象不再存在)。这肯定是一个错误,但是,既然你说错误,我怀疑这不是你想要的错误。
Visual Studio 中的一些标头 #define min 和 max。为了解决这个问题,请在最小值和最大值两边加上括号:
When compiling, I get:
That would be because you're returning a reference to a temporary (i.e. your object no longer exists by the time the caller of the function can check its value). This is certainly a bug but, since you said error, I suspect it isn't the bug you meant.
Some of the headers in Visual Studio #define min and max. To get around that, put parentheses around min and max:
gcc 4.2.1 给出了有关返回临时引用的警告,因此,将代码更改为按值返回会有所帮助。
顺便说一句:我还将
max
重命名为my_max
,因为max
已在
中定义gcc -Wall -Wextra file.cpp -o test
没有给出警告或错误,输出为:gcc 4.2.1 gives a warning about returning reference to a temporary, so, changing your code to return by value helps.
BTW: I also renamed
max
tomy_max
, sincemax
is already defined in<algorithm>
gcc -Wall -Wextra file.cpp -o test
gives no warnings or errors, output is: