Method call acting unexpectedly like an l-value

前端 未结 4 1209
遥遥无期
遥遥无期 2021-01-17 17:56

Can anybody explain why this code compiles:

typedef struct longlong
{
  unsigned long low;
  long high;
}
longlong;

typedef longlong Foo;    

struct FooStr         


        
4条回答
  •  走了就别回头了
    2021-01-17 18:28

    This works because longlong is a class, and as such = is longlong::operator =, the implicitly-defined assignment operator. And you can call member functions on temporaries as long as they're not qualified with & (the default operator = is unqualified).

    To restrict the assignment operator to lvalues, you can explicitly default it with an additional ref-qualifier:

    struct longlong
    {
        longlong &operator = (longlong const &) & = default;
        //                                      ^
    
        // ...
    };
    

提交回复
热议问题