Is it possible to return an instance of a non-movable, non-copyable type?

后端 未结 3 1791
长发绾君心
长发绾君心 2021-02-12 19:55

In VS2013 update 5, I\'ve got this:

class Lock
{
public:
    Lock(CriticalSection& cs) : cs_(cs)
    {}

    Lock(const Lock&) = delete;
    Lock(Lock&am         


        
3条回答
  •  不要未来只要你来
    2021-02-12 20:41

    If that compiles, it is a bug in the compiler. VC2015 correctly fails to compile it.

    class Foo
    {
    public:
        Foo() {}
        Foo(const Foo&) = delete;
        Foo(Foo&&) = delete;
    };
    
    
    Foo Bar()
    {
        return Foo();
    }
    

    Gives me:

    xxx.cpp(327): error C2280: 'Foo::Foo(Foo &&)': attempting to reference a deleted function
    

    and g++ 4.9 says:

    error : use of deleted function 'Foo::Foo(Foo&&)'
    

    The standard is very clear that a copy constructor or move constructor must exist and be accessible, even if RVO means it is not invoked.

提交回复
热议问题