Cppcoreguidelines: C.46 and copy/move ctors

Created on 10 Mar 2018  路  5Comments  路  Source: isocpp/CppCoreGuidelines

I told colleagues to take C.46 ("By default, declare single-argument constructors explicit") into account. The effect was that they made copy and move ctors (being ctors that take a single argument) explicit and then they ran into trouble for code like "const auto a = myclass{...}; auto b = a;".

Reading the title and the exceptions of C.46 it looks like copy/move ctors should be explicit too. Reading the reason in C.46 it looks like only conversion ctors are targeted.

It would be nice to make more clear what is meant. If copy/move ctors should not be explicit by default then they should be added to the exceptions paragraph.

Most helpful comment

I'm not even sure this should be an "Exception" rather than a note. You don't need an exception if they aren't covered by the rule in the first place, and the rule applies to converting constructors.

All 5 comments

Given that the reason is "To avoid unintended conversions." obviously it doesn't apply to constructors that don't convert anything. But since making copy/move constructors explicit causes major problems with most use cases, clearly saying the guideline doesn't apply to them might be wise.

@jwakely What do you think about this addition after the current Exception and before Enforcement?

Exception
Copy and move constructors should not be made explicit because this causes major problems with most use cases.

How about:

Exception
Copy and move constructors should not be made explicit because they do not perform conversions. Explicit copy/move constructors make passing and returning by value difficult.

I'm not even sure this should be an "Exception" rather than a note. You don't need an exception if they aren't covered by the rule in the first place, and the rule applies to converting constructors.

PR #1169 is merged, so I am closing this issue. Thanks @jwakely for the support.

Was this page helpful?
0 / 5 - 0 ratings