我有一个使用声明的嵌套模板链。它看起来像这样:
template <typename A, typename B, typename C, typename D>
class Foo {
public:
Foo() : value{0} {};
template <typename AC, typename BC, typename CC, typename DC>
Foo(const Foo<AC, BC, CC, DC>& rhs) : value{rhs.value} {}
template <typename AC, typename BC, typename CC, typename DC>
Foo& operator=(const Foo<AC, BC, CC, DC>& rhs) {
value = rhs.value;
return *this;
}
template <typename F>
F convertTo() {
return F(*this);
}
C value;
};
template <typename ThingC, typename ThingD>
using Bar = Foo<int, float, ThingC, ThingD>;
template <typename ThingD = long double>
using Bif = Bar<char, ThingD>;
以下情况给我带来了麻烦:
int main(int argc, char* argv[]) {
Bif bifDefault;
Bif<long> bifLong;
Bif<unsigned> bifUnsigned = bifDefault.convertTo<Bif<unsigned>>();
Bif<long double> bifLongDouble = bifLong.convertTo<Bif>(); // ERROR...
// expected a type, got 'Bif'
}
我在
F convertTo
生产线:
template argument deduction/substitution failed
。
还有最奇怪的错误,在错误行:
no instance of function template "Foo<A, B, C, D>::convertTo [ with A=int, B=float, C=char, D=long int]" matches the argument list
D=长整型
哦!这是怎么回事?把它改成双倍也不行。显然,默认模板参数正在传播到函数,但它正在这样做
错误的
对于long double,即使类型正确也不起作用。
我怎样才能让它工作?