Question

struct that
{
    that &frob()
    {
        return *this;
    }
    that frob() const
    {
        return that(*this);
    }

    //that &&frob() && //<-called only if *this is an rvalue
    //{
    //    return move(*this);
    //}

    that()
    {
        // make things
    }
    that(const that &other)
    {
        // copy things
    }
    that(that &&other)
    {
        // move things
    }
};

Obviously the function in comments above is not legal C++, but I need to know if there is a way to accomplish this:

that().frob().frob().frob();

and so on, while each call to frob() would effectively call the "move" version thereof. As this is something that can be determined at compile time, I can't think of any reason for it to not exist in some form.

I can write something like this:

that &&frob(that &&t)
{
    return t;
}

Which would result in this:

frob(frob(frob(that())));

Which is somewhat annoying to read and doesn't accomplish my goal of "spelling things out" with delegation.

Was it helpful?

Solution

If you want the && annotated function to play well with others, you should use the & annotation on the others.

that &frob() &
{
    return *this;
}
that frob() const &
{
    return that(*this);
}

OTHER TIPS

No, there is no way to determine if an object is an rvalue from within the object. The best you can do is copy it if it is const and move it if its not:

that frob() const
{
    return *this;
}

that frob()
{
    return std::move(*this);
}

EDIT

Further reaserch shows that you actually can, as @Potatoswatter pointed out, but its called ref-qualification and not annotation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top