【发布时间】:2019-12-10 05:59:26
【问题描述】:
考虑这个最小的例子
template <class T>
class Foo
{
public:
Foo(const T& t_)
: t(t_)
{
}
Foo(T&& t_)
: t(std::move(t_))
{
}
T t;
};
template <typename F>
Foo<F> makeFoo(F&& f)
{
return Foo<F>(std::forward<F>(f));
}
int main()
{
class C
{
};
C c;
makeFoo(c);
}
MSVC 2017 因 Foo 的 ctor 的重新定义错误而失败。显然,T 被推导出为 C& 而不是预期的 C。这究竟是如何发生的以及如何修改代码以使其执行预期的操作:从 const 引用复制构造 Foo::t 或从 r- 移动构造它价值。
【问题讨论】:
标签: c++ c++14 perfect-forwarding type-deduction