模板类型推导问题
我有一个 variant
类。它有一对构造函数:
/// Construct and fill.
template <typename T>
inline
variant (const T& t)
{
YYASSERT (sizeof (T) <= S);
new (buffer.raw) T(t);
}
template <typename T>
inline
variant (T&& t)
{
YYASSERT (sizeof (T) <= S);
new (buffer.raw) T(std::move(t));
}
现在我在这段代码中调用了这些构造函数:
parser::symbol_type
parser::make_IDENTIFIER (const Wide::ParsedFile::Identifier*& v)
{
return symbol_type (token::IDENTIFIER, v);
}
symbol_type
采用一个 variant
作为此特定构造函数中的第二个参数,并且 v
正在被隐式转换。
但是,MSVC 将尝试使用右值引用构造函数而不是使用其他构造函数,从而在尝试new
引用时导致编译错误。为什么会这样,我怎样才能让它停止?
I've got a variant
class. It has a pair of constructors:
/// Construct and fill.
template <typename T>
inline
variant (const T& t)
{
YYASSERT (sizeof (T) <= S);
new (buffer.raw) T(t);
}
template <typename T>
inline
variant (T&& t)
{
YYASSERT (sizeof (T) <= S);
new (buffer.raw) T(std::move(t));
}
Now I've called those constructors in this code:
parser::symbol_type
parser::make_IDENTIFIER (const Wide::ParsedFile::Identifier*& v)
{
return symbol_type (token::IDENTIFIER, v);
}
symbol_type
takes a variant
as it's second argument in this specific constructor, and v
is being implicitly converted.
However, MSVC will try to use the rvalue reference constructor instead of using the other constructor, resulting in a compilation error when it attempts to new
a reference. Why is that, and how can I make it stop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您不应重载模板化的
T&&
函数。相反,您应该使用单个转发函数:它具有两个重载的功能,同时避免选择错误的问题。
我相信(不是积极的)这些是您的过载集中的两个变体:
第二个获胜,因为它比第一个更专业(我正在做出有根据的猜测,我不是 100% 积极)。
You generally should not overload a templated
T&&
function. You should instead have the single function which forwards:This has the functionality of your two overloads, while avoiding the problem of picking the wrong one.
I believe (not positive) that these are the two variants in your overload set:
And the second one wins because it is more specialized than the first (I'm making an educated guess, I'm not 100% positive).
第二个模板会更匹配,因为 const 说明符位于函数和第一个构造函数中的不同位置。
在第一个重载中,您将 T 推导为
然后创建对该类型的 const 引用。这增加了一个额外的因素。
The second template would be a better match, because the
const
specifiers are in different places in your function and in the first constructor.In the first overload you will have T being deduced as
And then creating a const reference to that type. That adds an extra const.