1

I have some code here that I am compiling with -Weffc++ -Wall -Wextra.

Basically I have this snippet:

class base
{};

class test : public base
{
public:
    base& operator=(int)
    {
        return *this;
    }
};

and I get the warning: warning: 'operator=' should return a reference to '*this' [-Weffc++]. I am not really sure what to make of that warning. I have read that this is perfectly ok (i.e. to return a deferenced this).

Is there a way I can keep my complier happy?

Justin
  • 24,288
  • 12
  • 92
  • 142
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • Returning a reference to `*this` is pretty common, specially when overloading operators like `operator=`. – François Andrieux Sep 04 '18 at 18:20
  • 3
    Can you make a [mcve] that still gives the warning on your machine? – NathanOliver Sep 04 '18 at 18:21
  • 2
    Is `*this` an instance of `test`? Does not reproduce, live: https://godbolt.org/z/JvKVxq – Richard Critten Sep 04 '18 at 18:22
  • 1
    Did a search turn up documentation on weffc's warnings list? This is a "style police" option for g++ so these are not "real" warnings. You need to find why Meyers considers it poor style. – Dave S Sep 04 '18 at 18:22
  • @FrançoisAndrieux - infact that is what I was doing on my linux VM – code_fodder Sep 04 '18 at 18:24
  • @NathanOliver I can do that, but its on a secure network so I would have to hand tpye it. I wanted to use some online tool to provide the example + warning to make it easier to show : ( – code_fodder Sep 04 '18 at 18:25
  • @DaveS oh... some of the effc++ I thought looked quite useful.. maybe I should turn them on manually? – code_fodder Sep 04 '18 at 18:26
  • My crystal ball tells me that you are overloading `operator=` and not returning the same type as the class (meaning the code snippet you gave is irrelevant). Something like [this](https://godbolt.org/z/NxP0Yk) or even [this](https://godbolt.org/z/-5Kc5Z) – Justin Sep 04 '18 at 18:27
  • @Justin I will try to revise my example tomorrow based on my linux VM code... its using g++ v 7.4.0 c++11, etc... – code_fodder Sep 04 '18 at 18:27
  • @Justin hmm... that could be, it returns the type of the base class....ah... ok, that might be a problem!? .. gah, I will update the example when I get home in about an hour! sorry if that is it, but probably same question if/when it is :o – code_fodder Sep 04 '18 at 18:28
  • 2
    You shouldn't return a reference to a base class on operator=. The warning is correct and the only thing you could do about it is to either fix the code or ignore the warning. – Not a real meerkat Sep 04 '18 at 18:33
  • 2
    [Keep in mind that you should also consider if you really want to use -Weffc++ at all...](https://stackoverflow.com/a/11529328/3854787) – Not a real meerkat Sep 04 '18 at 18:38
  • @Justin just got home... thanks for that, yes that is exactly the problem. – code_fodder Sep 04 '18 at 19:06
  • @CássioRenan given that I did not write this little bit of code. I am just trying to fix the warning produced by other developers... what would be your advise to fix that? - return a pointer? – code_fodder Sep 04 '18 at 19:07
  • either follow P.E. Normand's answer (this is what I would do if I were in your position); ignore the warning (by just leaving it there - the compiler is your servant, not your master - or using pragmas); or completely disable it (I never used it, in fact). – Not a real meerkat Sep 04 '18 at 19:39
  • @CássioRenan thanks for the advice... at the moment there are only a few issues like the, but as you say, if it becomes a problem we may well disable it. But in this case I think it picked up a valid warning? – code_fodder Sep 04 '18 at 20:29

1 Answers1

6

Change your code to:

class test : public base
{
public:
     test& operator=(int)
     {
        return *this;
     }
};

And everybody will be happy, not just your compiler.

PS: If you wish to know more the warnings produced by -Weffc++ are an extract of the recommendations found in this book :

Effective C++: 55 Specific Ways to Improve Your Programs and Designs, Addison–Wesley, 1992, (ISBN 0-321-33487-6).

PilouPili
  • 2,601
  • 2
  • 17
  • 31