使用另一个模板 class 的嵌套名称说明符专门化模板 class
specialize a template class using a nested name specifier of another template class
我想使用另一个模板 class 的嵌套名称说明符来专门化模板 class。但是编译器抱怨它不能推导出这段代码。我该怎么办?
template <typename T>
struct convert{ // this is a class in an extern library
// the guide of this library tells me to specialize
// convert to my own class for some features.
void foo(T&t) {/* do something */}
};
template <typename T>
struct A{
struct A_sub{ // my class
};
};
template <typename T>
struct convert<A<T>::A_sub> { // error, compiler can't deduce
};
错误:class模板偏特化包含无法推导的模板参数;此部分特化将永远不会被使用 [-Wunusable-partial-specialization]
结构convert::A_sub> {
main.cpp:65:19: 注意:不可推导的模板参数 'T'
模板
产生了 1 个错误。
您需要 typename 关键字:
struct convert<typename A<T>::A_sub>
但这对你没有帮助,因为语言会阻止你想做的事情(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf temp.deduct.type):
The non-deduced contexts are:
—(5.1) The nested-name-specifier of a type that was specified using a qualified-id
想象一下这样的情况:
template <typename T> struct A {
using C = int;
};
template <typename W> struct Q;
template <typename W> struct Q<typename A<T>::C> { ... };
...
Q<int> q; // which argument T for A should be deduced and how compiler is supposed to guess?
我想使用另一个模板 class 的嵌套名称说明符来专门化模板 class。但是编译器抱怨它不能推导出这段代码。我该怎么办?
template <typename T>
struct convert{ // this is a class in an extern library
// the guide of this library tells me to specialize
// convert to my own class for some features.
void foo(T&t) {/* do something */}
};
template <typename T>
struct A{
struct A_sub{ // my class
};
};
template <typename T>
struct convert<A<T>::A_sub> { // error, compiler can't deduce
};
错误:class模板偏特化包含无法推导的模板参数;此部分特化将永远不会被使用 [-Wunusable-partial-specialization]
结构convert::A_sub> {
main.cpp:65:19: 注意:不可推导的模板参数 'T'
模板
产生了 1 个错误。
您需要 typename 关键字:
struct convert<typename A<T>::A_sub>
但这对你没有帮助,因为语言会阻止你想做的事情(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf temp.deduct.type):
The non-deduced contexts are:
—(5.1) The nested-name-specifier of a type that was specified using a qualified-id
想象一下这样的情况:
template <typename T> struct A {
using C = int;
};
template <typename W> struct Q;
template <typename W> struct Q<typename A<T>::C> { ... };
...
Q<int> q; // which argument T for A should be deduced and how compiler is supposed to guess?