<<操作员超载和模板专业化

发布于 2025-01-18 20:03:20 字数 939 浏览 3 评论 0原文

作为学习目的的一部分,我只是在使用模板专业化以及操作员超载的情况下播放

#include <iostream>

template<class T>
void operator<<(std::ostream& COUT,T val)
{
   std::operator<<(COUT,val); //works for basic data types
}

//specializing for const char*
template<>
void operator<<(std::ostream& COUT,const char* val)
{
    std::operator<<(COUT,val);
}
int main()
{
    std::cout<<"samplestring"; //getting error here
    return 0;
}

,我会遇到以下错误,但我无法弄清楚。

<source>:17:14: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'const char [13]')
   17 |     std::cout<<"samplestring";
      |     ~~~~~~~~~^~~~~~~~~~~~~~~~
      |          |     |
      |          |     const char [13]
      |          std::ostream {aka std::basic_ostream<char>}

这里有什么问题

谢谢..!

As part of study purpose, I was just playing with template specialization together with operator overloading

#include <iostream>

template<class T>
void operator<<(std::ostream& COUT,T val)
{
   std::operator<<(COUT,val); //works for basic data types
}

//specializing for const char*
template<>
void operator<<(std::ostream& COUT,const char* val)
{
    std::operator<<(COUT,val);
}
int main()
{
    std::cout<<"samplestring"; //getting error here
    return 0;
}

I am getting following error which I cannot figure out.

<source>:17:14: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'const char [13]')
   17 |     std::cout<<"samplestring";
      |     ~~~~~~~~~^~~~~~~~~~~~~~~~
      |          |     |
      |          |     const char [13]
      |          std::ostream {aka std::basic_ostream<char>}

what is the problem here

Thank you..!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情栀口红 2025-01-25 20:03:21

问题是已经有运算符&lt;(std :: Ostream&amp; cout,const char* val)由标准库定义。

因此,现在有两个符号,

std::operator<<(std::ostream& ostream,const char* val);
operator<<(std::ostream& COUT,const char* val); // YOURS

因为符号不同,没有多个定义错误。

呼叫std :: Cout&lt;&lt; “ Hello”,编译器搜索所有操作员&lt;>当前在呼叫站点上定义的功能。看来它只是您的函数,但是编译器还查看了定义传递参数的所有命名空间。在这种情况下,它是std ::名称空间,并且编译器在那里找到了STL版本。这称为参数依赖性查找(ADL)。

由于这两个功能都是模板,并且都不比另一个功能更专业,因此呼叫是模棱两可的。

请不要将所有大写字母用作变量cout,通常保留给宏,一个字母的模板参数为异常t

//为基本数据类型工作

也不正确的基本数据类型,std :: cout&lt;&lt; 5;不会使用t来调用您的定义。因为存在 std :: oStream :: oStream :: optream :: operator&lt&lt;&lt;&lt;&lt; v) 方法。同样的查找也会发生,但是由于此方法不是模板,因此它是一个更好的候选者,因此选择了。您的模板功能从未被调用。

The problem is there is already operator<<(std::ostream& COUT,const char* val) defined by the standard library.

So now there are two symbols

std::operator<<(std::ostream& ostream,const char* val);
operator<<(std::ostream& COUT,const char* val); // YOURS

Because the symbols are different, there are no multiple-definition errors.

For a call std::cout << "Hello", the compiler searches for all operator<< functions currently defined at the call site. That looks like it is just your function, but the compiler also looks into all namespaces in which the passed arguments are defined. In this case it is std:: namespace and there the compiler finds the STL version. That is called argument dependent lookup(ADL).

Since both functions are templates and neither is more specialized than the other, the call is ambiguous.

Please do not use all capital letters as variable COUT, that is usually reserved for macros, with one-letter template parameters being exception T.

//works for basic data types

That is also not true, std::cout<<5; will NOT call your definition with T. Because there exists std::ostream::operator<<(int v) method. Same lookup happens but since this method is not a template, it is a better candidate and thus chosen. Your template function is never called.

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