Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to disable constructor synthesizing on a class?

Tags:

c++

gcc

Suppose I have a class that I want to make sure my compiler (GCC in this case) doesn't synthesize any constructors or assignment methods for. I've found one way to do this which is to just include a const int member in the class, but this doesn't rub me to well. Is there an attribute or something which signifies this.

like image 834
Joe Doliner Avatar asked Aug 22 '11 17:08

Joe Doliner


2 Answers

If you define (or only declare) it yourself, then the compiler will not define it for you.

struct A
{
     A (); /*declaration is enough to prevent the compiler from 
             generating default constructor!*/
};

While the declaration is enough to prevent the compiler from generating default constructor, it is necessary to define it if your code requires the default constructor, otherwise you'll get linker error.

In C++11 (the new ISO Standard), you can disable constructors, copy-constructor, and copy-assignment as:

struct A
{
    A(const A&) = delete;            //disable copy-constructor
    A& operator=(const A&) = delete; //disable copy-assignment
};

Now the interesting part

You can also selectively disable constructor(s) for selected types which makes delete more interesting. Consider this,

struct A
{
       A (int) {}
};

Object of this class can be created not only with int argument, but any type which implicitly converts to int. For example,

A a1(10);  //ok
A a2('x'); //ok - char can convert to int implicitly

B b; 
A a3(b); //ok - assume b provides user-defined conversion to int

Now suppose, for whatever reason, I don't want the users of class A to create objects with char or class B , which fortunately or unfortunately can implicitly convert to int, then you can disable them as:

struct A
{
     A(int) {}
     A(char) = delete;      //disable
     A(const B&) = delete;  //disable
};

Now here you go:

A a1(10);  //ok
A a2('x'); //error

B b; 
A a3(b); //error - assume (even if) b provides user-defined conversion to int

Online Demo : http://ideone.com/ZVyK7

The error messages are very clear:

prog.cpp:9:5: error: deleted function 'A::A(char)'
prog.cpp:10:5: error: deleted function 'A::A(const B&)'

like image 169
Nawaz Avatar answered Oct 24 '22 21:10

Nawaz


The classical way is to declare them, but never implement. Most people would expect that declaration to be private or protected.

In C++0x, you can explicitly delete them. Which does pretty much the same thing, but is way nicer to read.

like image 29
Christopher Creutzig Avatar answered Oct 24 '22 21:10

Christopher Creutzig