<<操作员超载和模板专业化
作为学习目的的一部分,我只是在使用模板专业化以及操作员超载的情况下播放
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是已经有
运算符&lt;(std :: Ostream&amp; cout,const char* val)
由标准库定义。因此,现在有两个符号,
因为符号不同,没有多个定义错误。
呼叫
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
Because the symbols are different, there are no multiple-definition errors.
For a call
std::cout << "Hello"
, the compiler searches for alloperator<<
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 isstd::
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 exceptionT
.That is also not true,
std::cout<<5;
will NOT call your definition withT
. Because there existsstd::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.