Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


667. Trivial special member functions that cannot be implicitly defined

Section:11.4.5.3  [class.copy.ctor]    Status:CD2    Submitter:James Widman    Date:14 December 2007

[Voted into WP at March, 2010 meeting as part of document N3079.]

Should the following class have a trivial copy assignment operator?

    struct A {        int& m;        A();        A(const A&);    };

11.4.5.3 [class.copy.ctor] paragraph 11 does not mention whether thepresence of reference members (or cv-qualifiers, etc.) should affecttriviality. Should it?

One reason why this matters is that implementations have to makethe builtin type trait operator__has_trivial_default_ctor(T)work so that they can support the type trait templatestd::has_trivial_default_constructor.

Assuming the answer is “yes,” it looks like we probablyneed similar wording for trivial default and trivial copy ctors.

Notes from the February, 2008 meeting:

Deleted special member functions are also not trivial. Resolutionof this issue should be coordinated with the concepts proposal.

Notes from the June, 2008 meeting:

It appears that this issue will be resolved by the conceptsproposal directly. The issue is in “review” status tocheck if that is indeed the case in the final version of theproposal.

Additional notes (May, 2009):

Consider the following example:

    struct Base {      private:        ~Base() = default;    };    struct Derived: Base {    };

The implicitly-declared destructor ofDerived isdefined as deleted becauseBase::~Base() is inaccessible,but it fulfills the requirements for being trivial. PresumablytheBase destructor should be non-trivial, either bydirectly specifying that it is non-trivial or by specifying that itis user-provided. An alternative would be to make it ill-formed toattempt to declare a defaulted non-public special member function.

Any changes to the definition of triviality should be checkedagainst Clause 11 [class] paragraph 6 for any changes neededthere to accommodate the new definitions.

Notes from the July, 2009 meeting:

The July, 2009 resolution ofissue 906addresses the example above (with an inaccessible defaulteddestructor): a defaulted special member function can only havenon-public access if the defaulted definition is outside the class,making it non-trivial. The example as written above would beill-formed.

Proposed resolution (October, 2009):

  1. Change 9.6 [dcl.fct.def] paragraph 9 as follows:

  2. ...Only special member functions may be explicitly defaulted.Explicitly-defaulted functions and implicitly-declared functions arecollectively calleddefaulted functions, and theimplementation shalldefine them as if they hadprovide implicit definitionsfor them(11.4.5 [class.ctor], 11.4.7 [class.dtor], 11.4.5.3 [class.copy.ctor]), which might mean defining them as deleted. Aspecial member function that would be implicitly defined as deletedmay be explicitly defaulted only on its first declaration, in whichcase it is defined as deleted. A special member function isuser-provided if it is user-declared and not explicitlydefaulted on its first declaration. A user-providedexplicitly-defaulted function is defined at the point where it isexplicitly defaulted. [Note:...
  3. Change 11.4.5 [class.ctor] paragraphs 5-6 as follows:

  4. Adefault constructor for a classX is aconstructor of classX that can be called without anargument. If there is no user-declared constructor for classX, a constructor having no parameters is implicitly declaredas defaulted (9.6 [dcl.fct.def]). Animplicitly-declared default constructor is aninline publicmember of its class.A default constructor istrivial if it is not user-provided (9.6 [dcl.fct.def])and if:

    An implicitly-declareddefaulted defaultconstructor for classX is defined as deleted if:

    A default constructor is trivial if it is neitheruser-provided nor deleted and if:

    Otherwise, the default constructor isnon-trivial.

    Anon-user-provided default constructorfor aclassthat is defaulted and not deleted isimplicitly defined when it is used (6.3 [basic.def.odr])to create an object of its class type (6.8.2 [intro.object]),or when it is explicitly defaulted after its first declaration.The implicitly-definedor explicitly-defaulted defaultconstructor performs the set of initializations of the class thatwould be performed by a user-written default constructor for thatclass with noctor-initializer (11.9.3 [class.base.init])and an emptycompound-statement. If that user-written defaultconstructor would be ill-formed, the program is ill-formed. If thatuser-written default constructor would satisfy the requirements of aconstexpr constructor (9.2.6 [dcl.constexpr]), theimplicitly-defined default constructor is constexpr. Before thenon-user-provideddefaulted default constructorfor a class is implicitly defined, all the non-user-provided defaultconstructors for its base classes and its non-static data membersshall have been implicitly defined. [Note: animplicitly-declared default constructor has anexception-specification (14.5 [except.spec]). Anexplicitly-defaulted definition has no implicitexception-specification. —end note]

  5. Change 11.4.7 [class.dtor] paragraphs 3-4 as follows:

  6. If a class has no user-declared destructor, a destructor isdeclared implicitlydeclared as defaulted(9.6 [dcl.fct.def]). An implicitly-declared destructor isaninline public member of its class.If the class is aunion-like class that has a variant member with a non-trivialdestructor, an implicitly-declared destructor is defined as deleted(9.6 [dcl.fct.def]). A destructor istrivial if it isnot user-provided and if:

    An implicitly-declareddefaulted destructorfor a classX is defined as deleted if:

    A destructor is trivial if it is neither user-provided nordeleted and if:

    Otherwise, the destructor isnon-trivial.

    Anon-user-provided destructorthat is defaultedand not defined as deleted isimplicitly defined when itis used to destroy an object of its class type (6.8.6 [basic.stc]), or when it is explicitly defaulted after its firstdeclaration.A program is ill-formed if the class for which adestructor is implicitly defined or explicitly defaulted has:

    Before thenon-user-provideddefaulteddestructor for a class is implicitly defined, all thenon-user-definednon-user-provided destructorsfor its base classes and its non-static data members shall have beenimplicitly defined. [Note: an implicitly-declared destructorhas anexception-specification (14.5 [except.spec]). Anexplictly defaulted definition has no implicitexception-specification. —end note]

  7. Change 11.4.5.3 [class.copy.ctor] paragraphs 4-9 as follows:

  8. If the class definition does not explicitly declare a copyconstructor, one isdeclaredimplicitlyimplicitly declared as defaulted (9.6 [dcl.fct.def]). Thus...

    ...An implicitly-declared copy constructor is aninlinepublic member of its class. An implicitly-declareddefaulted copy constructor for a classX isdefined as deleted ifX has: ...

    A copy constructor for classX istrivialtrivial if it isnotneither user-providednor deleted(9.6 [dcl.fct.def]) and if...

    Anon-user-provided copy constructorthat isdefaulted and not defined as deleted isimplicitlydefined if it is used to initialize an object of its class typefrom a copy of an object of its class type or of a class type derivedfrom its class type116, or when it is explicitlydefaulted after its first declaration. [Note: the copyconstructor is implicitly defined even if the implementation elidedits use (6.8.7 [class.temporary]). —end note]

    Before thenon-user-provideddefaulted copyconstructor for a class is implicitly defined, all non-user-providedcopy constructors...

    The implicitly-definedor explicitly-defaulted copyconstructor for a non-union classX performs...

    The implicitly-definedor explicitly-defaulted copyconstructor for a unionX copies the object representation(6.9 [basic.types]) ofX.

  9. Change 11.4.5.3 [class.copy.ctor] paragraphs 11-15 as follows:

  10. If the class definition does not explicitly declare a copyassignment operator, one isdeclaredimplicitlyimplicitly declared as defaulted (9.6 [dcl.fct.def])...

    ...An implicitly-declareddefaulted copyassignment operator for classX is defined as deleted ifX has:...

    A copy assignment operator for classX istrivialif it isnotneither user-providednordeleted and if...

    Anon-user-provided copy assignment operatorthatis defaulted and not defined as deleted isimplicitlydefined when an object of its class type is assigned a value ofits class type or a value of a class type derived from its classtype, or when it is explicitly defaulted after its firstdeclaration.

    Before thenon-user-provideddefaulted copyassignment operator for a class is implicitly defined...

    The implicitly-definedor explicitly-defaulted copyassignment operator for a non-union classX performs...

    It is unspecified whether subobjects representing virtual baseclasses are assigned more than once by the implicitly-definedorexplicitly-defaulted copy assignmentoperator. [Example:...

    The implicitly-definedor explicitly-defaulted copyassignment operator for a unionX copies the objectrepresentation (6.9 [basic.types]) ofX.




[8]ページ先頭

©2009-2026 Movatter.jp