return type of deleted functions in c++11
Asked Answered
R

3

6

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?

Reconcilable answered 3/2, 2017 at 10:4 Comment(3)
don't see why not. function overloads are selected by argument types, not return types.Coen
If by "work" you mean "have the compiler refuse to compile because you are using a deleted function", then yes.Agglomerate
yes. will the compiler delete functions based on argument types/signature, rather than return types? in another word, the compiler will delete "WHATEVER operator=(const A&)"?Reconcilable
R
4

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.

Retha answered 3/2, 2017 at 11:8 Comment(0)
I
4

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.

Inwards answered 3/2, 2017 at 10:11 Comment(0)
R
4

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.

Retha answered 3/2, 2017 at 11:8 Comment(0)
T
0

compiler deduces return type from selected overload. If overload is explicitly deleted, compilation fails before return type is taken into account.

Throaty answered 3/2, 2017 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.