c++ {*this} inside curly braces

Viewed 4735

The following code compiles fine:

g++ -std=c++11 test.cpp -Wall -Wextra -Wfatal-errors && ./a.out

However, if I remove the curly braces from {*this} and use *this instead, I will face with error:

error: use of deleted function ‘Obj::Position::Position(Obj::Position&&)’

What is the difference between {*this} and *this?

class Obj
{
    template<bool> friend class Position;

    double data;
public:
    class Position
    {
        const Obj& ref;
    public:
        inline Position(const Obj& ref): ref(ref){}
        inline Position(Position const &) = delete;
        inline Position(Position &&) = delete;
    };
    inline Obj(){}
    inline Obj(const double &data): data(data){}
    inline auto get_pos() const-> Position{return {*this};} /* <--- here */
    inline auto get_pos()-> Position{return {*this};}
};

int main()
{
    return 0;
}
4 Answers
Related