Automatically generated class member functions in C ++ 11 in case of classes without any constructors

In C ++ 11, I define a structure of the following general kind:

struct MyStruct
{
    static void myFunc( void );

    //constructors
    MyStruct( void ) = delete;
};

      

Unable to create object of type MyStruct

because default constructor is marked as deleted. Even a method myFunc

cannot instantiate a class. The structure is still in use because it MyStruct::myFunc

is public

and can be called from outside.

Now my question is, since it is not possible to create any type object MyStruct

, does the compiler even bother to generate code for the copy constructor, address operator, or destructor ?

As a side note: in the case of my actual code, I actually need to implement the functionality in terms of static member functions of the class, since I have to use partial template specialization to emulate the partial template specialized function. So I am wondering how I can keep the class as thin as possible.

Edit : Removed a note about auto-generated address operators as per @ Praetorian's comment and answer.

+3


source to share


1 answer


The compiler will implicitly declare both a destructor and a copy constructor for your class, but since you have no way to instantiate the class, your program will never be able to use the odr destructor or copy constructor because of which will not be implicitly defined.

§12.4 [class.dtor]

4 If the class does not have a user-declared destructor, the destructor is implicitly declared by default (8.4). An implicitly declared destructor is a member of inline public

its class.

5 & ​​nbsp; A destructor, which is by default and not defined as deleted, is implicitly defined when it uses odr (3.2) to destroy an object of its class type (3.7) or when it explicitly defaults after its first declaration.



§12.8 [class.copy]

7 If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If a class definition declares a move constructor or moves an assignment operator, the implicitly declared copy constructor is determined to be deleted; otherwise, it is defined as default (8.4). The latter case is deprecated if the class has a custom copy assignment operator or a user-declared destructor.

13 The copy / move constructor, which is by default and not defined as remote, is implicitly defined if it is used by odr (3.2) or when it is explicitly defaulted after it was first declared.

As far as the address ( operator&

) operator is concerned , the compiler never generates this operator overload for you implicitly. An inline operator address can be applied to any lvalue for its address, but to a language function and is not associated with overloading that operator for a user-defined type.

+5


source







All Articles