移动和模板构造函数,错误的一个
move and templated constructor, wrong one is taken
我需要一个类,该类需要复制/移动和一个模板构造函数,如以下示例:
class A
{
public:
A() {}
A( A&& ){std::cout << "Move" << std::endl;}
A( const A& ){ std::cout << "copy" << std::endl;}
template < typename T>
A( T&& ) { std::cout << "any" << std::endl; }
};
class B: public A
{
public:
B():A() {}
B( const B& b): A(b) {}
B( B&& b): A( std::move(b)) {}
};
int main()
{
B b;
B b2( b ); // expected call copy
B b3( std::move( b )); // expected call to move
}
,但我遇到了模板的构造函数,这对我来说绝对是如此。为什么移动构造函数不是更好的匹配?
在这里找到了一些提示:模板构造函数优先于普通副本并移动构造函数?
在其中一条评论中,我看到Sfinae有可能。但是我看不到任何有效的实现来选择正确的构造函数。有人可以给我一个提示以拿起正确的构造函数吗?
我希望通过移动构造函数上升层次结构。因此,如何实现将选择正确的基类之一的移动构造函数。因此,我希望在这里输出"移动"而不是"任何"。
编辑:除了给定答案外,以下解决方案似乎也有效:
class B: public A
{
public:
B():A() {}
B( const B& b ): A( (const A&)b ) {}
D( B&& b): A( std::forward<A>(b)) {}
};
也解释了基类中的模板为何"吃"构造函数调用,因为模板不需要从b到a的隐式铸件,因此模板是更好的匹配。如果以前施放了明确的效果,则一切正常。
in
A( std::move(b))
std::move(b)
为您提供了B&&
。执行过载分辨率后,会找到A( A&& )
和(从模板中(A( B&& )
。模板是一个完全匹配的,因此它会选择。
要解决此问题,只需摆脱模板,就不需要它。如果您确实需要在实际代码中需要它,则可以在模板构造函数上使用Sfinae,以阻止使用
派生的类调用它。template < typename T, std::enable_if_t<!std::is_convertible_v<T*, A*>, bool> = true>
A( T&& ) { std::cout << "any" << std::endl; }
相关文章:
- 试图修复一个错误,该错误不会让我开始编程其余部分
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 我正在尝试一个傻瓜 C++ 练习,我遇到了一个错误,说类 'GraduateStudent' 没有任何名为 'advisor' 的字段
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- 在Cython中使用C库时,我遇到了一个错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 我试图用c++编写递归fibonacci序列,但当我编译时,我遇到了一个错误
- 从system()调用G++会返回一个错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 使用声明:GCC 和 Clang 的另一个错误?
- 全球免费给出一个错误.调试器不解释
- 相对于继承的构造函数,gcc 编译器是否还有一个错误?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- 为什么第三板有一个错误
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 使用本地类型声明的G lambda被使用但从未定义 - 确实是一个错误
- 我需要在 Android Studio 中构建 NDK. 但它返回一个错误