Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defaulted copy constructor and copy assignment assignment operator giving strange error

I have defaulted my copy constructor and copy assignment operator as follows:

  Config(const Config& config) = default;
  Config& operator=(const Config& rhs) = default;

and then have given access to these via friendship to a free standing function. Upon creating a copy of a config object, I'm getting the following warning and note(?):

./cfg/config.hpp:129:3: warning: unused parameter 'config' [-Wunused-parameter] cfg/get.cpp: In function 'const cfg::Config& cfg::Get(bool)': cfg/get.cpp:34:30: note: synthesized method 'cfg::Config::Config(const cfg::Config&)' first required here

It would appear the copy constructor isn't even being instantiated. Despite this, the code seems to run.

gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

like image 341
goji Avatar asked Jan 15 '23 09:01

goji


2 Answers

Just omit the parameters:

Config(const Config&) = default;
Config& operator=(const Config&) = default;

The statements above instruct the compiler to generate default special member functions of the type stated, but the details of these implementations are not specified. The parameter names are redundant and have no effect. The compiler is free to issue warnings for legal code.

From §8.4.2:

... A function that is explicitly defaulted shall

— be a special member function,

— have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const T”, where T is the name of the member function’s class) as if it had been implicitly declared ...

like image 97
juanchopanza Avatar answered Jan 30 '23 22:01

juanchopanza


I just ran into this today; while I'm just using the "omit the names" workaround, there are two relevant bugs open against GCC:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50895 -- a suggestion to omit the "unused parameter" warning on virtual methods (or maybe just empty virtual methods).

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57211 -- this problem (unused param names on defaulted methods), complicated by the fact that the line/column error indicator points at the top of the class defn, instead of at the defaulted method defn.

As can be seen in my comments to the bugs, my interest is in making my documentation more consistent. Ideally I could have my code and my docs both build without warnings.

If nothing else, the warning here is IMHO incorrect: the parameter is certainly used, it's just used by a different name or through some other mechanism.

like image 44
AnthonyFoiani Avatar answered Jan 30 '23 22:01

AnthonyFoiani