使用模板参数重载解析
Overload resolution with template parameters
我无法理解为什么以下会导致模棱两可的调用:
#include <iostream>
// generic version f(X, Y)
template <class X, class Y>
void f(X x, Y y) {
std::cout << "generic" << std::endl;
}
// overload version
template <class X>
void f(X x, typename X::type y) {
std::cout << "overload" << std::endl;
}
struct MyClass {
using type = int;
};
int main() {
f(MyClass(), int()); // Call to f is ambiguous
}
我希望重载版本比通用版本更专注于第二个参数,被选为最佳候选者。我知道如果我将重载版本更改为
template <class X>
void f(X x, int y) {
std::cout << "overload" << std::endl;
}
然后调用就很好地解决了,这意味着它与 X::type 是模板相关名称这一事实有关,但仍然无法弄清楚它失败的原因。非常感谢任何帮助。
首先,我们挑选可行的候选人。它们是:
void f(MyClass, int); // with X=MyClass, Y=int
void f(MyClass, typename MyClass::type); // with X=MyClass
这些候选项都采用相同的参数,因此具有等效的转换序列。所以 none 基于这些的决胜局适用,所以我们回到 [over.match.best] 中的最后一个可能的决胜局:
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 and F2 are function template specializations, and the function template for F1 is more specialized
than the template for F2 according to the partial ordering rules described in 14.5.6.2.
所以我们尝试根据偏序规则对两个函数模板进行排序,这涉及为每个模板参数合成一个唯一的类型,并尝试对每个过载执行模板推导。但是使用来自 [temp.deduct.partial] 的关键附加相关规则:
Each type nominated above from the parameter template and the corresponding type from the argument
template are used as the types of P and A. If a particular P contains no template-parameters that participate
in template argument deduction, that P is not used to determine the ordering.
那么这是什么意思。首先,让我们尝试从重载中推导出通用版本。我们选择合成类型 UniqueX
(对于 X
)和 UniqueX_type
(对于 typename X::type
),看看我们是否可以调用泛型函数。这成功了(使用 X=UniqueX
和 Y=typename X::type
)。
让我们试试反过来。我们选择一个 UniqueX
(对于 X
)和一个 UniqueY
(对于 Y
)并尝试执行模板推导。对于第一个 P/A 对,这很容易成功。但是对于第二个参数,X
是一个非推导上下文,您会认为这意味着模板推导失败。 BUT 根据报价的粗体部分,我们只是为了订购而跳过这个 P/A 对。因此,由于第一个 P/A 对成功,我们认为整个推导过程已经成功。
由于模板推导在两个方向上都成功,我们不能选择一个函数或另一个函数更专业。由于没有进一步的决胜局,因此没有单一的最佳可行候选人,因此这个决定是模棱两可的。
当第二次重载改为:
template <class X> void f(X, int);
过程中发生变化的部分是演绎现在在一个方向上失败了。我们可以推导出 X=UniqueX
但是第二对有一个 int
类型的参数和一个 UniqueY
类型的参数,这是行不通的,所以这个方向失败了。反过来,我们可以推导出X=UniqueX
和Y=int
。这使得此重载比通用重载更专业,因此我最初提到的最后一个决胜局更喜欢它。
作为附录,请注意模板的部分排序很复杂。考虑:
template <class T> struct identity { using type = T; };
template <class T> void foo(T ); // #1
template <class T> void foo(typename identity<T>::type ); // #2
template <class T> void bar(T, T); // #3
template <class T> void bar(T, typename identity<T>::type ); // #4
foo(0); // calls #1, #2 isn't even viable
foo<int>(0); // calls #2
bar(0,0); // calls #3! we fail to deduce 3 from 4, but we succeed
// in deducing 4 from 3 because we ignore the second P/A pair!
我无法理解为什么以下会导致模棱两可的调用:
#include <iostream>
// generic version f(X, Y)
template <class X, class Y>
void f(X x, Y y) {
std::cout << "generic" << std::endl;
}
// overload version
template <class X>
void f(X x, typename X::type y) {
std::cout << "overload" << std::endl;
}
struct MyClass {
using type = int;
};
int main() {
f(MyClass(), int()); // Call to f is ambiguous
}
我希望重载版本比通用版本更专注于第二个参数,被选为最佳候选者。我知道如果我将重载版本更改为
template <class X>
void f(X x, int y) {
std::cout << "overload" << std::endl;
}
然后调用就很好地解决了,这意味着它与 X::type 是模板相关名称这一事实有关,但仍然无法弄清楚它失败的原因。非常感谢任何帮助。
首先,我们挑选可行的候选人。它们是:
void f(MyClass, int); // with X=MyClass, Y=int
void f(MyClass, typename MyClass::type); // with X=MyClass
这些候选项都采用相同的参数,因此具有等效的转换序列。所以 none 基于这些的决胜局适用,所以我们回到 [over.match.best] 中的最后一个可能的决胜局:
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 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2.
所以我们尝试根据偏序规则对两个函数模板进行排序,这涉及为每个模板参数合成一个唯一的类型,并尝试对每个过载执行模板推导。但是使用来自 [temp.deduct.partial] 的关键附加相关规则:
Each type nominated above from the parameter template and the corresponding type from the argument template are used as the types of P and A. If a particular P contains no template-parameters that participate in template argument deduction, that P is not used to determine the ordering.
那么这是什么意思。首先,让我们尝试从重载中推导出通用版本。我们选择合成类型 UniqueX
(对于 X
)和 UniqueX_type
(对于 typename X::type
),看看我们是否可以调用泛型函数。这成功了(使用 X=UniqueX
和 Y=typename X::type
)。
让我们试试反过来。我们选择一个 UniqueX
(对于 X
)和一个 UniqueY
(对于 Y
)并尝试执行模板推导。对于第一个 P/A 对,这很容易成功。但是对于第二个参数,X
是一个非推导上下文,您会认为这意味着模板推导失败。 BUT 根据报价的粗体部分,我们只是为了订购而跳过这个 P/A 对。因此,由于第一个 P/A 对成功,我们认为整个推导过程已经成功。
由于模板推导在两个方向上都成功,我们不能选择一个函数或另一个函数更专业。由于没有进一步的决胜局,因此没有单一的最佳可行候选人,因此这个决定是模棱两可的。
当第二次重载改为:
template <class X> void f(X, int);
过程中发生变化的部分是演绎现在在一个方向上失败了。我们可以推导出 X=UniqueX
但是第二对有一个 int
类型的参数和一个 UniqueY
类型的参数,这是行不通的,所以这个方向失败了。反过来,我们可以推导出X=UniqueX
和Y=int
。这使得此重载比通用重载更专业,因此我最初提到的最后一个决胜局更喜欢它。
作为附录,请注意模板的部分排序很复杂。考虑:
template <class T> struct identity { using type = T; };
template <class T> void foo(T ); // #1
template <class T> void foo(typename identity<T>::type ); // #2
template <class T> void bar(T, T); // #3
template <class T> void bar(T, typename identity<T>::type ); // #4
foo(0); // calls #1, #2 isn't even viable
foo<int>(0); // calls #2
bar(0,0); // calls #3! we fail to deduce 3 from 4, but we succeed
// in deducing 4 from 3 because we ignore the second P/A pair!