了解 C++模板错误

发布于 2024-08-20 11:36:40 字数 680 浏览 3 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

愁杀 2024-08-27 11:36:40

当您说:

 max (max(a,b), c)

max(char*,char*) 返回一个 BY VALUE 指针。然后,您返回对此值的引用。为了实现这项工作,您应该使所有 max() 函数返回值而不是引用,正如我认为在对您上一个问题的回答中所建议的那样,或者使 char* 重载接受并返回对指针的引用。

When you say:

 max (max(a,b), c)

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.

无需解释 2024-08-27 11:36:40

您正在返回对临时对象的引用。 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.

尹雨沫 2024-08-27 11:36:40

如果您确实想返回引用,那么您必须修改第二个重载以也返回引用。

char const* const & max (char const* const & a, char const* const & b)

If you really want to return a reference, then you have to modify the second overload to return a reference too.

char const* const & max (char const* const & a, char const* const & b)
梦过后 2024-08-27 11:36:40

您的示例与此等效,也许您会这样看得更好:

int foo()
{
    return 0;
}

int const & bar()
{
    return foo(); // Reference to what???
}

Your example is equivalent to this, maybe you'll see it better this way:

int foo()
{
    return 0;
}

int const & bar()
{
    return foo(); // Reference to what???
}
つ可否回来 2024-08-27 11:36:40

编译时,我得到:

maxtest.cpp: In function `const T& max(const T&, const T&, const T&) [with T = const char*]':
maxtest.cpp:29:   instantiated from here
maxtest.cpp:19: warning: returning reference to temporary

那是因为您返回了对临时对象的引用(即,当函数的调用者可以检查其值时,您的对象不再存在)。这肯定是一个错误,但是,既然你说错误,我怀疑这不是你想要的错误。

Visual Studio 中的一些标头 #define min 和 max。为了解决这个问题,请在最小值和最大值两边加上括号:

return (max) ( (max) (a, b), c );

When compiling, I get:

maxtest.cpp: In function `const T& max(const T&, const T&, const T&) [with T = const char*]':
maxtest.cpp:29:   instantiated from here
maxtest.cpp:19: warning: returning reference to temporary

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:

return (max) ( (max) (a, b), c );
硪扪都還晓 2024-08-27 11:36:40

gcc 4.2.1 给出了有关返回临时引用的警告,因此,将代码更改为按值返回会有所帮助。

顺便说一句:我还将 max 重命名为 my_max,因为 max 已在 中定义

#include <iostream>
#include <cstring>
#include <string>

template <typename T>
inline T const my_max (T const& a, T const& b)
{
    return  a < b  ?  b : a;
}

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

template <typename T>
inline T const my_max (T const& a, T const& b, T const& c)
{
    return my_max (my_max(a,b), c);  // error
}

int main ()
{
    std::cout << my_max(7, 42, 68) << "\n"; // OK

    const char* s1 = "frederic";
    const char* s2 = "anica";
    const char* s3 = "lucas";
    std::cout << my_max(s1, s2, s3) << "\n"; // NO ERROR

}

gcc -Wall -Wextra file.cpp -o test 没有给出警告或错误,输出为:

68
卢卡斯

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 to my_max, since max is already defined in <algorithm>

#include <iostream>
#include <cstring>
#include <string>

template <typename T>
inline T const my_max (T const& a, T const& b)
{
    return  a < b  ?  b : a;
}

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

template <typename T>
inline T const my_max (T const& a, T const& b, T const& c)
{
    return my_max (my_max(a,b), c);  // error
}

int main ()
{
    std::cout << my_max(7, 42, 68) << "\n"; // OK

    const char* s1 = "frederic";
    const char* s2 = "anica";
    const char* s3 = "lucas";
    std::cout << my_max(s1, s2, s3) << "\n"; // NO ERROR

}

gcc -Wall -Wextra file.cpp -o test gives no warnings or errors, output is:

68
lucas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文