模板不起作用的默认参数
我有一条使用声明的嵌套模板链。它看起来像这样:
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
line上也会遇到一个错误:template参数扣除/替换失败
。
最奇怪的错误,在错误行中:
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 int !这是怎么回事?将其更改为双倍也无法正常工作。显然,默认模板参数正在传播到该函数,但是它正在执行错误的,即使它确实正确地使其变得正确,也无法正常工作。
我该如何工作?
I have a chain of nested templated using declarations. It looks something like this:
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>;
The following gives me trouble:
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'
}
I also get an error at the F convertTo
line : template argument deduction/substitution failed
.
And the weirdest error, at the ERROR line:
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 int! What's going on here? Changing it to just double doesn't work either. Clearly, the default template argument is propagating through to the function, but it's doing it wrong for long double, and doesn't work even when it does get the type correct.
How do I get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
bif
不是一种类型,它是模板
d类型。当您声明bifdefault
时,您正在摆脱它ctad ,但是当您调用convertto
> convertto 时,它不适用(您不是传递非类型template
parameter)。用biflong.convertto&lt; bif&lt;&gt;&gt;()
替换为预期的代码。The issue is that
Bif
isn't a type, it's atemplate
d type. You're getting away with it when you declarebifDefault
because of CTAD, but it doesn't apply when you callconvertTo
(you're not passing a non-typetemplate
parameter). The code compiles as expected when replaced withbifLong.convertTo<Bif<>>()
.