移动和模板构造函数,错误的一个

move and templated constructor, wrong one is taken

本文关键字:一个 错误 构造函数 移动      更新时间:2023-10-16

我需要一个类,该类需要复制/移动和一个模板构造函数,如以下示例:

    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; }
相关文章: