可变参数模板的部分特化
考虑以下类模板“X”及其部分特化。
template <class ...Types>
struct X {}; // #1
template <class T1>
struct X<T1> {}; // #2
template <class T1, class ...Types>
struct X<T1, Types...> {}; // #3
X<int> x; // #2 or #3 ?
我怀疑 X
很明显#2和#3都比#1更专业,现在比较#2和#3。根据14.5.5.2,让我们考虑以下#2'和#3'中哪一个更专业。
template <class T1>
void f(X<T1>); // #2'
template <class T1, class ...Types>
void f(X<T1, Types...>); // #3'
根据14.8.2.4,第一步是使用#2'作为实参模板,#3'作为形参模板进行模板实参推导。假设唯一的参数类型是X
A = X<A1>, P = X<T1, Types...> => T1 = A1, Types = {}
第二步是使用 #3' 作为参数模板和 #2' 作为参数模板来完成。给定唯一的参数类型是 X
A = X<A1, Args...>, P = X<T1> => T1 = A1 (Args is ignored)
最终双向论证推演成功。所以#2 和#3 一样专业。总之,X int 是是模棱两可的。
我的问题是:“我的解释正确吗?”
如果这个解释是正确的,那么 20.9.7.6/3 中 'std::common_type' 的定义是不合适的。
template <class ...T>
struct common_type; // #1
template <class T>
struct common_type<T> // #2
{
typedef T type;
};
template <class T, class U>
struct common_type<T, U> // #3
{
typedef
decltype(true ? declval<T>() : declval<U>())
type;
};
template <class T, class U, class ...V>
struct common_type<T, U, V...> // #4
{
typedef typename
common_type<typename common_type<T, U>::type, V...>::type
type;
};
当common_type<A,B>时使用时,#3 和#4 是不明确的。
注意:在第一个示例中,GCC 4.7.0(快照)和 Clang 3.0 选择#2。然而,这些编译器非常不可靠,以至于它们不遵循 N3281 的其他更改。
Consider the following class template 'X' and its partial specializations.
template <class ...Types>
struct X {}; // #1
template <class T1>
struct X<T1> {}; // #2
template <class T1, class ...Types>
struct X<T1, Types...> {}; // #3
X<int> x; // #2 or #3 ?
I suspect X<int> is ambiguous. It is because:
It is obvious that both #2 and #3 are more specialized than #1, #2 and #3 are now compared. According to 14.5.5.2, let's consider which of the following #2' and #3' is more specialized.
template <class T1>
void f(X<T1>); // #2'
template <class T1, class ...Types>
void f(X<T1, Types...>); // #3'
According to 14.8.2.4, the first step is the template argument deduction using #2' as the argument template and #3' as the parameter template. Given the only argument type is X<A1>, the deduced T1 is A1, and Types is empty.
A = X<A1>, P = X<T1, Types...> => T1 = A1, Types = {}
The second step is done using #3' as the argument template and #2' as the parameter template. Given the only argument type is X<A1, Args...>, according to 14.8.2.5/9 (note that this paragraph is recently revised by N3281), Args is simply ignored, the deduced T1 is A1 and argument deduction succeeds.
A = X<A1, Args...>, P = X<T1> => T1 = A1 (Args is ignored)
Finally, the bidirectional argument deductions succeeded. So #2 is just as specialized as #3. In conclusion, X<int> is ambiguous.
My question is: "is my interpretation correct?"
If this interpretation is correct, the definition of 'std::common_type' in 20.9.7.6/3 is inappropriate.
template <class ...T>
struct common_type; // #1
template <class T>
struct common_type<T> // #2
{
typedef T type;
};
template <class T, class U>
struct common_type<T, U> // #3
{
typedef
decltype(true ? declval<T>() : declval<U>())
type;
};
template <class T, class U, class ...V>
struct common_type<T, U, V...> // #4
{
typedef typename
common_type<typename common_type<T, U>::type, V...>::type
type;
};
When common_type<A, B> is used, #3 and #4 are ambiguous.
Note: on the first example, GCC 4.7.0 (snapshot) and Clang 3.0 select #2. However, these compilers are so unreliable that they don't follow the other changes by N3281.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
14.8.2.4,第 11 节(我参考草案 N3242)。
在您的情况下,将使用#3。
14.8.2.4, section 11 (I refer to draft N3242).
In your case, #3 will be used.