Reputation: 661
In C++11, I define a struct of the following general form:
struct MyStruct
{
static void myFunc( void );
//constructors
MyStruct( void ) = delete;
};
It is not possible to create any object of type MyStruct
as the default constructor is marked deleted. Even the method myFunc
can't create an instance of the class. The struct is still of use though, as MyStruct::myFunc
is public
and can be called from outside.
My question now is: Since it is not possible to create any object of type MyStruct
, will the compiler even bother to create code for a copy constructor, address operator or destructor?
Just as a remark: In the case of my actual code, I really have to implement functionality in terms of static class member functions, as I have to make use of partial template class specialization to emulate partial template function specialization. So I wonder how I can keep the class as slim as possible.
Edit: removed the remark about automatically generated address operators in accordance with the comment and answer of @Praetorian.
Upvotes: 3
Views: 1136
Reputation: 109189
The compiler will implicitly declare both a destructor and copy constructor for your class, but since you have no way of creating an instance of the class, your program will never be able to odr-use either the destructor or copy constructor, because of which neither will be implicitly defined.
§12.4 [class.dtor]
4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (8.4). An implicitly declared destructor is an
inline public
member of its class.
5 A destructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) to destroy an object of its class type (3.7) or when it is explicitly defaulted 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 the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.
13 A copy/move constructor that is defaulted and not defined as deleted is implicitly defined if it is odr-used (3.2) or when it is explicitly defaulted after its first declaration.
As for the address operator (operator&
), the compiler never generates that operator overload for you implicitly. The built-in address of operator can be applied to any lvalue type to take its address, but that's a language feature, and unrelated to overloading that operator for a user defined type.
Upvotes: 5