473,503 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

explicit default constructor

Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn't find where the standard stated this rule. Is it
really true? Where is it stated?

Thanks,

Marcelo Pinto.
Jul 22 '05 #1
12 4728
Marcelo Pinto wrote:
In practice, what is the diference between a default constructor and
an explicit default constructor?
None, IIUIC.

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
explicit Ae() {}
};

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn't find where the standard stated this rule. Is it
really true? Where is it stated?


No, it's not really true. At least it's not completely true. If you
declare a parameterized c-tor, there will be no default c-tor declared
implicitly. The implicit copy c-tor is still going to be provided.

See Standard, 12.1/5 "If there is no user-declared constructor for
class X, a default constructor is implicitly declared."

Victor
Jul 22 '05 #2
Marcelo Pinto wrote:

Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};

If you mark a constructor as explicit, then the compiler cannot
use that constructor under the hood to adjust types. At the moment
I cannot think of a case where one wants to do this with a default
constructor that takes no arguments.
But

class A
{
public:
A( int i_ = 0 ) : i( i_ ) {}
protected:
int i;
};

void foo( A Arg )
{
}

int main()
{
foo( 5 );
}

Here the compiler uses the constructor to create an A object which
can be used to call foo. If you mark the constructor as explicit,
then this is no longer allowed. You would need to write

int main()
{
foo( A(5) );
}

and thus create the A object explicitely.

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn't find where the standard stated this rule. Is it
really true?
No.

If you don't declare any constructor, the compiler will declare
a default constructor.
If you don't declare a copy constructor, the compiler will declare
a copy constructor too.

So:
any constructor declared (with the exception of the copy constructor)
-> the compiler will not declare a default constructor

you declare a copy constructor -> the compiler will no longer declare
a copy constructor for you.
Where is it stated?


Section 12 and 12.1 is fine

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

"Marcelo Pinto" <mp****@brturbo.com> wrote in message
news:7a**************************@posting.google.c om...
Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};
I don't think the second is legal. Explicit is for constructors that can be
called with one argument and it prevents that constructor from being used to
implicitly convert the type of the argument to the type of the class.

struct A
{
A();
A(int);
};

struct B
{
B();
explicit B(int);
};

A a;
B b;

a = 2; // legal
b = 2; // illegal constructor is explicit
b = B(2); // legal, constructor is used explicitly

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn't find where the standard stated this rule. Is it
really true? Where is it stated?


Its true for the default constructor, it is not true for the copy
constructor. This have nothing to do with the explicit keyword that you
mentioned above.

john
Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2m************@uni-berlin.de...

"Marcelo Pinto" <mp****@brturbo.com> wrote in message
news:7a**************************@posting.google.c om...
Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};


I don't think the second is legal. Explicit is for constructors that can

be

I do agree that it doesn't make much sense to declare explicit here. But
what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and Comeau
online don't seem to think it as illegal. Could you please quote the
standard.

-Sharad
Jul 22 '05 #5
> >
I don't think the second is legal. Explicit is for constructors that can

be

I do agree that it doesn't make much sense to declare explicit here. But
what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and Comeau
online don't seem to think it as illegal. Could you please quote the
standard.


I did say 'I don't *think* its legal'. I wasn't sure on the point and I
didn't think it was particularly important so I didn't look it up. I'm quite
happy to accept that the code is in fact legal (though senseless as you
say).

The main issue with the OP's post was that he seemed to have got mixed up
between explicitly defined constructors (as opposed to implicit compiler
generated constructors) and the explicit keyword when used on a constructor
and that was the point I tried to address in my reply.

john
Jul 22 '05 #6
Sharad Kala wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2m************@uni-berlin.de...
"Marcelo Pinto" <mp****@brturbo.com> wrote in message
news:7a**************************@posting.google .com...
Hi all,

In practice, what is the diference between a default constructor and
an explicit default constructor?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
};


I don't think the second is legal. Explicit is for constructors that can


be

I do agree that it doesn't make much sense to declare explicit here. But
what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and Comeau
online don't seem to think it as illegal. Could you please quote the
standard.


John is incorrect. The Standard explicitly _allows_ the default
c-tor to be declared 'explicit' (no pun intended). See 12.3.1/2

Victor
Jul 22 '05 #7
Victor Bazarov wrote:
I do agree that it doesn't make much sense to declare explicit here.
But what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and
Comeau online don't seem to think it as illegal. Could you please
quote the standard.


John is incorrect. The Standard explicitly _allows_ the default
c-tor to be declared 'explicit' (no pun intended). See 12.3.1/2


I wonder why the standard explicitly (also no pun intended) states that
"such a constructor will be used to perform default-initialization". I
mean, that's what default constructors generally do, not just when
declared 'explicit'.

Jul 22 '05 #8
Rolf Magnus wrote in news:ce*************@news.t-online.com in
comp.lang.c++:
Victor Bazarov wrote:
I do agree that it doesn't make much sense to declare explicit here.
But what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and
Comeau online don't seem to think it as illegal. Could you please
quote the standard.


John is incorrect. The Standard explicitly _allows_ the default
c-tor to be declared 'explicit' (no pun intended). See 12.3.1/2


I wonder why the standard explicitly (also no pun intended) states that
"such a constructor will be used to perform default-initialization". I
mean, that's what default constructors generally do, not just when
declared 'explicit'.


Presumably its pointing out that it has no effect i.e you can still do:

explicit_default_type value;

And don't have to do:

explicit_default_type value = explicit_default_type();

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2m************@uni-berlin.de>...

I don't think the second is legal. Explicit is for constructors that can be

I do agree that it doesn't make much sense to declare explicit here. But
what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and Comeau
online don't seem to think it as illegal. Could you please quote the
standard.


I did say 'I don't *think* its legal'. I wasn't sure on the point and I
didn't think it was particularly important so I didn't look it up. I'm quite
happy to accept that the code is in fact legal (though senseless as you
say).

The main issue with the OP's post was that he seemed to have got mixed up
between explicitly defined constructors (as opposed to implicit compiler
generated constructors) and the explicit keyword when used on a constructor
and that was the point I tried to address in my reply.


I wasn't mixing the two :). I tried to find an explanation of the
consequences of using the keyword _explicit_ with a default
constructor and the threads I found talked about the implicit
declaration/definition of constructors and I could not find the
circunstances in which this wasn't allowed to happen (reading the
standard), so I asked the other question.
john


Thanks.

Marcelo Pinto
Jul 22 '05 #10
Victor Bazarov <v.********@comAcast.net> wrote in message news:<Et**************@newsread1.dllstx09.us.to.ve rio.net>...
Marcelo Pinto wrote:
In practice, what is the diference between a default constructor and
an explicit default constructor?
None, IIUIC.

Sorry, what is IIUIC?

class Ai
{
public:
Ai() {}
};

class Ae
{
public:
explicit Ai() {}
explicit Ae() {}


I always make these silly mistakes :(
};

Another question. I read in another thread that, if you provide
explicitly one constructor, the implementation shall not implicitly
declare/define the default constructor and the copy constructor
(12.1), but I couldn't find where the standard stated this rule. Is it
really true? Where is it stated?
No, it's not really true. At least it's not completely true. If you
declare a parameterized c-tor, there will be no default c-tor declared
implicitly. The implicit copy c-tor is still going to be provided.

See Standard, 12.1/5 "If there is no user-declared constructor for
class X, a default constructor is implicitly declared."


Thank you. I read it more than once and always read it as if it stated
"If there is no user-declared *default* constructor for class X, a
default constructor is implicitly declared." Thats why I couldn't see
:( Victor


Thank you very much.

Marcelo Pinto
Jul 22 '05 #11
Rolf Magnus wrote:
...
I do agree that it doesn't make much sense to declare explicit here.
But what makes you say that the code is illegal ? VC 7.1, g++ 3.1 and
Comeau online don't seem to think it as illegal. Could you please
quote the standard.


John is incorrect. The Standard explicitly _allows_ the default
c-tor to be declared 'explicit' (no pun intended). See 12.3.1/2


I wonder why the standard explicitly (also no pun intended) states that
"such a constructor will be used to perform default-initialization". I
mean, that's what default constructors generally do, not just when
declared 'explicit'.
...


One thing that should be taken into account here is that default
constructor is not necessarily a constructor without parameters. Default
constructor is a constructor that can be invoked without specifying any
arguments. It can have any number of parameters, as long as all of them
have default arguments. This means that a constructor can be both
default constructor and conversion constructor at the same time.
Applying the keyword 'explicit' to such dual-role constructor makes
perfect sense.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #12
Marcelo Pinto wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<Et**************@newsread1.dllstx09.us.to.ve rio.net>...
Marcelo Pinto wrote:
In practice, what is the diference between a default constructor and
an explicit default constructor?
None, IIUIC.


Sorry, what is IIUIC?


If I Understand It Correctly
[...]


V
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1509
by: Dave | last post by:
Hello all, 1. I am able to declare as explicit ctors that cannot take exactly one argument - they either take fewer (default ctor) or more arguments. What is the significance of declaring such...
9
2351
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
8
2965
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
2
2129
by: Dave | last post by:
Hello NG, Can anybody fathom the purpose of an explicit copy constructor? On page 232 of the Josuttis STL reference, I see a reference to such. How could you ever need to supress the...
1
2243
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
1
1303
by: Greg | last post by:
Referring to Managed: why do templates require an explicit copy constructor to be defined in order to use gcnew whereas simply declaring an object as a local (still uses managed heap of course)...
2
3918
by: Fred Zwarts | last post by:
Consider the following code: // Start of code class MyException_t { public: // Default constructor.
12
8958
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
12
7173
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.