Should implicitly generated assignment operators be & ref-qualified?

后端 未结 2 1953
你的背包
你的背包 2020-12-03 11:32

The following code compiles without problem on gcc 4.8.1:

#include 

struct foo
{
};

int main()
{
    foo bar;

    foo() = bar;
    foo() =          


        
2条回答
  •  伪装坚强ぢ
    2020-12-03 12:22

    It seems like your question is more "Why would assignment to an rvalue ever be useful?" rather than "Why doesn't the standard ref-qualify auto generated constructors?"

    The reason assignment to an rvalue is allowed is because there are some cases where it is useful.

    One example usage is with std::tie (link):

    #include 
    #include 
    
    int main()
    {
        std::set s;
    
        std::set::iterator iter;
        bool inserted;
    
        // unpacks the return value of insert into iter and inserted
        std::tie(iter, inserted) = s.insert(7);
    }
    

    Example borrowed from cppreference.com then modified

提交回复
热议问题