Can a class can have several default constructor functions? I was wondering because I would think that one of anything can only have one default function by definition of "default", but I'm not too sure.
- 
        7If it had several default constructors, which would it choose?Qaz– Qaz2013-06-17 22:45:32 +00:00Commented Jun 17, 2013 at 22:45
- 
        1By a default constructor do you mean one that accepts no parameters? If so then that can't have more than one as how would the compiler tell which one you meant?T. Kiley– T. Kiley2013-06-17 22:45:58 +00:00Commented Jun 17, 2013 at 22:45
- 
        5@T.Kiley, By definition, it's one that can be called with no arguments. That still means default arguments are allowed.Qaz– Qaz2013-06-17 22:46:32 +00:00Commented Jun 17, 2013 at 22:46
- 
        @chris Well yeah, but I thought I'd confirm the definition in case there is a misunderstanding :PT. Kiley– T. Kiley2013-06-17 22:47:19 +00:00Commented Jun 17, 2013 at 22:47
                    
                        Add a comment
                    
                 | 
            
                
            
        
         
    1 Answer
A class can have several default constructors. However in that case, you cannot default-construct it because when trying to do so, you'd run into an ambiguity:
class C
{
public:
  C(); // a default constructor
  C(int = 0); // another default constructor
};
C c1; // error: ambiguity; both C::C() or C::C(int) with the default argument 0 match
C c2(0); // OK, no ambiguity
Note however that you cannot have two constructors with the same signature:
class C2
{
public:
  C2() {}
  C2() {} // error: C2::C2() already defined
};
8 Comments
Kerrek SB
 In my compiler (GCC 4.8.1), 
  std::is_default_constructible is false for your example.  According to the documentation, the trait checks whether "the class has an accessible default constructor". Either the compiler or that website are wrong, or this is somehow UB.Qaz
 @KerrekSB, It's defined in terms of 
  std::is_constructible<T>::value and the closest thing I can find is § 20.9.4.3/6, but I'm not sure if that's the right one to be using.Qaz
 @BenVoigt, Good thing it can quickly and easily be edited and proven with a reference, unlike another site I'm thinking of.
  Ben Voigt
 @chris: That's the right section in the Standard.  
  std::is_default_constructible<C> indicates whether C c; is well-formed.  It isn't (because of ambiguity), so std::is_default_constructible<C> is rightly false even though default constructors exist and are accessible.Qaz
 @KerrekSB, That would be the section I referenced. I was just slightly thrown off by the use of 
  std::add_rvalue_reference. Basically, std::is_default_constructible boils down to T t(); being well-formed, except when simplified like that, it becomes a function, which is explicitly prevented in the wording below. | 
 


