15

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.

Joe Doliner
  • 2,058
  • 2
  • 15
  • 19

2 Answers2

33

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&)'

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Don't want to spam comments but this answer is just too awesome!! – ithenoob Oct 13 '13 at 02:33
  • 1
    @ithenoob: Thanks. You can see this also: [What's the exact semantics of a deleted function in C++11?](http://stackoverflow.com/questions/19265920/whats-the-exact-semantics-of-a-deleted-function-in-c11) – Nawaz Oct 13 '13 at 03:15
15

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.

Christopher Creutzig
  • 8,656
  • 35
  • 45
  • 1
    Note also that boost has a `boost::noncopyable` class that can be used as a private base in a class that wants to delete the copy constructor (and `operator=`) – bdonlan Aug 22 '11 at 18:04