众所周知,仅在返回类型上有所不同的普通函数不能在C ++中超载。
但是,此限制不适合过载函数模板,例如:
int f(auto) { return 1; }
auto f(auto) { return 2; }
所有编译器都接受它,演示:
为什么该语言为此提供了这样的例外模板?
如果重载函数的返回类型有所不同,则可以使用cast到预期功能类型选择其中一个功能。令人惊讶的是,Clang允许人们解决歧义,即使返回类型实际上相同,例如:
((int(*)(int))f)(3);
选择
int f(auto) { return 1; }
演示:“ noreferrer”> https://gcc.godbolt.org/z/snfvbq1me
是错误的 这里?
It is well known that ordinary functions that differ only in their return type cannot be overloaded in C++.
But this limitation does not hold for overloaded function templates, for example:
int f(auto) { return 1; }
auto f(auto) { return 2; }
All compilers accept it, demo: https://gcc.godbolt.org/z/qj73Mzehd
Why does the language make such exception for the templates?
If the return type of the overloaded functions differ, then it will be possible to select one of the functions using the cast to expected function type. Surprisingly, Clang allows one to resolve the ambiguity even if the return type is actually the same, for example:
((int(*)(int))f)(3);
selects
int f(auto) { return 1; }
Demo: https://gcc.godbolt.org/z/snfvbq1ME
Is Clang wrong here?
发布评论
评论(1)
你的意思是吗?
是的,返回类型在那里。这总是使诸如
Sfinae之类的可能的原因是为什么签名中包含返回类型的原因。在返回类型中可能发生替换故障,因此这是签名比较的一部分。您仍然可能会产生两个相互矛盾的专业,以使其过载(在一般情况下,不是在示例中),但是模板会有所不同。
You mean this?
Yes, the return type is there. It's what always made possible things like
SFINAE is why the return type is included in the signature. Substitution failure is possible in the return type, so it's part of signature comparison. You can still potentially generate two conflicting specializations to overload on (in the general case, not in the example), but the templates would be different.