我想使用另一个模板类的嵌套名称说明符来专门化模板类。但是编译器抱怨它无法推断出这段代码。我该怎么办?
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
};
错误:类模板的部分专业化包含无法推导的模板参数;这种局部专业化永远不会使用[-Wunusable-partial-specialization]
struct convert :: A_sub> {
main.cpp:65:19:注意:不可推导的模板参数'T'
模板
产生了1个错误。
答案 0 :(得分:4)
您需要typename关键字:
struct convert<typename A<T>::A_sub>
但这不会帮到您,因为语言阻止了您想要做的事情(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf temp.deduct.type):
非推论上下文是:
-(5.1)使用限定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?