简体   繁体   English

在具有相同签名的模板化功能和非模板化功能之间进行选择时,为什么没有歧义?

[英]Why no ambiguity when choosing between templated and non-templated functions with same signatures?

The following code passes asserts: 以下代码传递断言:

int foo() { return 1; }

template<typename T>
int foo() { return 2; }

int main() {
  assert( 1 == foo() );
  assert( 2 == foo<int>() );
  return 0;
}

But to my understanding, according to Paragraph 13.3.3/1 of the C++11 Standard: 但是据我了解,根据C ++ 11标准的13.3.3 / 1段:

[...] Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i , ICSi(F1) is not a worse conversion sequence than ICSi(F2) , and then [...] F1 is a non-template function and F2 is a function template specialization [...] [...]给定这些定义,如果对于所有自变量iICSi(F1)转换顺序都不比ICSi(F2)差,则一个可行函数F1被定义为比另一个可行函数F2更好的函数,然后[ ...] F1是非模板函数, F2是函数模板专门化[...]

It should not, because signatures end up to be the same. 不应该这样,因为签名最终是相同的。 So why is there no ambiguity when foo<int>() is called? 那么为什么在调用foo<int>()时没有歧义呢? What am I missing? 我想念什么?

The text you quote is rather dense; 您引用的文本比较密集; you have to read it carefully. 您必须仔细阅读。 "F1 is better than F2 if for all arguments i , ICSi(F1) is not a worse conversion sequence than ICSi(F2)" -- that's true here, since the two conversion sequences are the same, hence, neither is worse than the other. “如果对于所有自变量i ,ICSi(F1)的转换顺序都不比ICSi(F2) ,则F1优于F2” –在这里是对的,因为两个转换顺序相同,因此,两个转换顺序都不比F2 差。其他。 So now you move to the last part: " and then F1 is a non-template function and F2 is a function template specialization". 因此,现在转到最后一部分:“ 然后 F1是非模板函数,而F2是函数模板特化”。 That's true, so F1 is a better match than F2. 没错,因此F1比F2更好。 Substituting foo() and foo<int>() for F1 and F2, respectively, the rule says that foo() is a better match than foo<int>() . 规则分别用foo()foo<int>()代替F1和F2,该规则表示foo()foo<int>()更好。

Whoops, I answered the wrong question. 糟糕,我回答了错误的问题。 As the comment points out, the question is, why does explicitly calling foo<int>() not resolve to foo() ? 正如评论所指出的那样,问题是,为什么显式调用foo<int>()无法解析为foo() And the answer is, foo<int>() is a call to an explicit template instantiation, not a call to an overloaded function. 答案是foo<int>()是对显式模板实例化的调用,而不是对重载函数的调用。 Consider: 考虑:

template <class Ty>
void f(Ty) { }

void f(int);
void g(int);

f(3.14);      // calls f<double> (overloaded function call)
f(1);         // calls f(int) (overloaded function call)
f<int>(3.14); // calls f<int> (explicit call of template instantiation)
g(3.14);      // calls g(int)

In this example, f<int> is the name of a template specialization. 在此示例中, f<int>是模板专门化的名称。 It is not the general function named f , so there is no overloading to consider, just like the call to g(3.14) . 不是名为f的通用函数,因此无需考虑重载,就像对g(3.14)的调用一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么非模板化函数具有相同的名称和参数但返回类型不同是非法的? (但模板功能合法吗?) - Why is it illegal for non-templated functions to have same name and arguments but different return types? (but legal for template functions?) 模板成员函数不能从同一类C ++中调用非模板成员函数 - Templated member functions cannot call non-templated member function from same class, C++ C ++:调用模板化类的非模板化函数 - C++: call non-templated functions of a templated class C++ 隐式转换规则为什么以及如何区分模板转换函数和非模板转换函数? - Why and how do the C++ implicit conversion rules distinguish templated conversion functions from non-templated? 如何专门化非模板类的模板成员函数? - how to specialize templated member functions of non-templated classes? 在模板化和非模板化代码之间进行接口时替换switch语句 - Replacing switch statements when interfacing between templated and non-templated code 从具有模板化构造函数的非模板化类继承 - 如何解决歧义? - Inheriting from a non-templated class that has a templated constructor - how to resolve ambiguity? “重载”模板化和非模板化类型 - “Overloading” templated and non-templated types 非模板函数的尾随要求子句的编译器差异 - Compiler variance for trailing requires-clauses on non-templated functions C ++在调用时指定函数的非模板版本 - C++ specify non-templated version of function when calling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM