Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return type of deleted functions in c++11

In c++ 11, we could disable copy constructor and assignment operator, with delete:

class A {
  A(const A&) = delete;
  A& operator=(const A&) = delete;
}

One day, my colleague use the void return type rather than the reference.

class A {
  A(const A&) = delete;
  void operator=(const A&) = delete;
}

Is this one also ok?

e.g., if i have

A a, b, c;
a = b = c;

will this work?

like image 281
pepero Avatar asked Feb 03 '17 10:02

pepero


2 Answers

Return types are not a part of the function signature in c++ (this is also why you can't overload functions only by the return type). So it's ok because your deleted function will still be found during the name lookup. You might have compiler warnings though, depending on your compiler version/settings.

like image 174
SingerOfTheFall Avatar answered Nov 15 '22 06:11

SingerOfTheFall


The return type of operator= is arbitrary. Returning a reference to *this or void is conventional. You could return int* or std::string. The conventional solution is usually better.

A deleted function cannot be called, and expressions deriving its return type are at the least substitution failures, so I see no standard-defined harm.

Compilers may or may not give worse diagnostics from the void return value than the reference one. Compare the a=b=c case in the two cases for your compiler; noise about "cannot assign from void" would make me dislike returning void from the deleted version.

like image 29
Yakk - Adam Nevraumont Avatar answered Nov 15 '22 05:11

Yakk - Adam Nevraumont