Hello everyone,


I am reading the C++ Programming Language book, but can not find the
function of explicit keyword of constructor. Could anyone explain its usage
or refer some learning materials please?


thanks in advance,
George

Re: explicit keyword by Alex

Alex
Tue Dec 11 00:59:40 PST 2007

"George" wrote:
> I am reading the C++ Programming Language book, but can
> not find the
> function of explicit keyword of constructor. Could anyone
> explain its usage
> or refer some learning materials please?

"explicit (C++)"
http://msdn2.microsoft.com/en-us/library/h1y7x448(VS.90).aspx


HTH
Alex



Re: explicit keyword by George

George
Tue Dec 11 01:16:02 PST 2007

Thanks Alex,


What benefits we could get if we use explicit keyword to prevent from
implicit conversion? Could you show some practical usage scenarios please?


regards,
George

"Alex Blekhman" wrote:

> "George" wrote:
> > I am reading the C++ Programming Language book, but can
> > not find the
> > function of explicit keyword of constructor. Could anyone
> > explain its usage
> > or refer some learning materials please?
>
> "explicit (C++)"
> http://msdn2.microsoft.com/en-us/library/h1y7x448(VS.90).aspx
>
>
> HTH
> Alex
>
>
>

Re: explicit keyword by =?iso-8859-1?Q?Lidstr=F6m?=

=?iso-8859-1?Q?Lidstr=F6m?=
Tue Dec 11 02:31:42 PST 2007

On Tue, 11 Dec 2007 01:16:02 -0800, George wrote:

> Thanks Alex,
>
>
> What benefits we could get if we use explicit keyword to prevent from
> implicit conversion? Could you show some practical usage scenarios please?

The std::auto_ptr class has an explicit constructor. This is because
auto_ptr assumes ownership and will delete the pointer it owns upon
destruction. If you don't understand that you are creating an auto_ptr, you
will be surprised. An example:

void f(std::auto_ptr<int> p);

int main()
{
int* p = new int;

f(p); <-- doesn't compile
// p would be deleted inside f, but you might have missed that
// if auto_ptr didn't have an explicit constructor.

f(std::auto_ptr<int>(p)); <-- compiles
// this time you should know that p is deleted inside f
}

Hope this helps!

--
Daniel

Re: explicit keyword by Bo

Bo
Tue Dec 11 02:59:56 PST 2007

George wrote:
:: Thanks Alex,
::
::
:: What benefits we could get if we use explicit keyword to prevent
:: from implicit conversion? Could you show some practical usage
:: scenarios please?

It is used when you just don't want an implicit conversion. :-)

What's wrong with the examples in the book?


Bo Persson

::
:: "Alex Blekhman" wrote:
::
::: "George" wrote:
:::: I am reading the C++ Programming Language book, but can
:::: not find the
:::: function of explicit keyword of constructor. Could anyone
:::: explain its usage
:::: or refer some learning materials please?
:::
::: "explicit (C++)"
::: http://msdn2.microsoft.com/en-us/library/h1y7x448(VS.90).aspx
:::
:::
::: HTH
::: Alex




Re: explicit keyword by George

George
Tue Dec 11 03:12:00 PST 2007

Thanks Daniel,


I think the benefit is brought to developers, which could warn them
(compile) that function f accepts auto_ptr other than normal pointer, right?


regards,
George

"Daniel Lidström" wrote:

> On Tue, 11 Dec 2007 01:16:02 -0800, George wrote:
>
> > Thanks Alex,
> >
> >
> > What benefits we could get if we use explicit keyword to prevent from
> > implicit conversion? Could you show some practical usage scenarios please?
>
> The std::auto_ptr class has an explicit constructor. This is because
> auto_ptr assumes ownership and will delete the pointer it owns upon
> destruction. If you don't understand that you are creating an auto_ptr, you
> will be surprised. An example:
>
> void f(std::auto_ptr<int> p);
>
> int main()
> {
> int* p = new int;
>
> f(p); <-- doesn't compile
> // p would be deleted inside f, but you might have missed that
> // if auto_ptr didn't have an explicit constructor.
>
> f(std::auto_ptr<int>(p)); <-- compiles
> // this time you should know that p is deleted inside f
> }
>
> Hope this helps!
>
> --
> Daniel
>

Re: explicit keyword by George

George
Tue Dec 11 03:13:04 PST 2007

Sorry Bo,


What book do you mean?


regards,
George

"Bo Persson" wrote:

> George wrote:
> :: Thanks Alex,
> ::
> ::
> :: What benefits we could get if we use explicit keyword to prevent
> :: from implicit conversion? Could you show some practical usage
> :: scenarios please?
>
> It is used when you just don't want an implicit conversion. :-)
>
> What's wrong with the examples in the book?
>
>
> Bo Persson
>
> ::
> :: "Alex Blekhman" wrote:
> ::
> ::: "George" wrote:
> :::: I am reading the C++ Programming Language book, but can
> :::: not find the
> :::: function of explicit keyword of constructor. Could anyone
> :::: explain its usage
> :::: or refer some learning materials please?
> :::
> ::: "explicit (C++)"
> ::: http://msdn2.microsoft.com/en-us/library/h1y7x448(VS.90).aspx
> :::
> :::
> ::: HTH
> ::: Alex
>
>
>
>

Re: explicit keyword by Giovanni

Giovanni
Tue Dec 11 03:33:20 PST 2007


"George" <George@discussions.microsoft.com> ha scritto nel messaggio
news:E87122B7-A72C-4028-9FF4-FDCB08F4B8F9@microsoft.com...

> What book do you mean?

I think Bo means the book you cited in your first post in this thread, i.e.:
C++ Programming Language

Giovanni



Re: explicit keyword by Bo

Bo
Tue Dec 11 03:44:36 PST 2007

George wrote:
:: Sorry Bo,
::
::
:: What book do you mean?

The book that you are supposed to be reading. :-)

The C++ Programming Language has a section 11.7.1 Explicit
Constructors, which explains some use of the keyword. Have you read
that yet?


Bo Persson


::
::
:: regards,
:: George
::
:: "Bo Persson" wrote:
::
::: George wrote:
::::: Thanks Alex,
:::::
:::::
::::: What benefits we could get if we use explicit keyword to prevent
::::: from implicit conversion? Could you show some practical usage
::::: scenarios please?
:::
::: It is used when you just don't want an implicit conversion. :-)
:::
::: What's wrong with the examples in the book?
:::
:::
::: Bo Persson
:::
:::::
::::: "Alex Blekhman" wrote:
:::::
:::::: "George" wrote:
::::::: I am reading the C++ Programming Language book, but can
::::::: not find the
::::::: function of explicit keyword of constructor. Could anyone
::::::: explain its usage
::::::: or refer some learning materials please?
::::::
:::::: "explicit (C++)"
:::::: http://msdn2.microsoft.com/en-us/library/h1y7x448(VS.90).aspx
::::::
::::::
:::::: HTH
:::::: Alex




Re: explicit keyword by George

George
Tue Dec 11 03:55:01 PST 2007

Hi Bo,


I have finally find it. From the book, I only see how to use explicit
keyword -- its function. But I have not seen any practical scenarios why we
should use explicit keyword. Any experience sharing from you are welcome. :-)


regards,
George

"Bo Persson" wrote:

> George wrote:
> :: Sorry Bo,
> ::
> ::
> :: What book do you mean?
>
> The book that you are supposed to be reading. :-)
>
> The C++ Programming Language has a section 11.7.1 Explicit
> Constructors, which explains some use of the keyword. Have you read
> that yet?
>
>
> Bo Persson
>
>
> ::
> ::
> :: regards,
> :: George
> ::
> :: "Bo Persson" wrote:
> ::
> ::: George wrote:
> ::::: Thanks Alex,
> :::::
> :::::
> ::::: What benefits we could get if we use explicit keyword to prevent
> ::::: from implicit conversion? Could you show some practical usage
> ::::: scenarios please?
> :::
> ::: It is used when you just don't want an implicit conversion. :-)
> :::
> ::: What's wrong with the examples in the book?
> :::
> :::
> ::: Bo Persson
> :::
> :::::
> ::::: "Alex Blekhman" wrote:
> :::::
> :::::: "George" wrote:
> ::::::: I am reading the C++ Programming Language book, but can
> ::::::: not find the
> ::::::: function of explicit keyword of constructor. Could anyone
> ::::::: explain its usage
> ::::::: or refer some learning materials please?
> ::::::
> :::::: "explicit (C++)"
> :::::: http://msdn2.microsoft.com/en-us/library/h1y7x448(VS.90).aspx
> ::::::
> ::::::
> :::::: HTH
> :::::: Alex
>
>
>
>

Re: explicit keyword by George

George
Tue Dec 11 03:55:01 PST 2007

Thanks Giovanni,


I have finally find it. From the book, I only see how to use explicit
keyword -- its function. But I have not seen any practical scenarios why we
should use explicit keyword. Any experience sharing from you are welcome. :-)


regards,
George

"Giovanni Dicanio" wrote:

>
> "George" <George@discussions.microsoft.com> ha scritto nel messaggio
> news:E87122B7-A72C-4028-9FF4-FDCB08F4B8F9@microsoft.com...
>
> > What book do you mean?
>
> I think Bo means the book you cited in your first post in this thread, i.e.:
> C++ Programming Language
>
> Giovanni
>
>
>

Re: explicit keyword by Duane

Duane
Tue Dec 11 04:14:44 PST 2007


"George" <George@discussions.microsoft.com> wrote in message
news:5920D19C-EBD0-4EDD-BDA4-59B0E855BC29@microsoft.com...
> Hi Bo,
>
>
> I have finally find it. From the book, I only see how to use explicit
> keyword -- its function. But I have not seen any practical scenarios why
> we
> should use explicit keyword. Any experience sharing from you are welcome.
> :-)
>

Creating a ctor with the explicit keyword prevents it being used with an
implicit cast. There are times when you don't want an implicit cast.

Consider if you have multiple ctors that treat numbers. Maybe you
want to handle a short int differently from a long double.

Or maybe you want to prevent your ctor from being used with
any but one type arg.



Re: explicit keyword by George

George
Tue Dec 11 04:22:01 PST 2007

Thanks Duane,


I do not quite understand this point (and agree with other points).

> Consider if you have multiple ctors that treat numbers. Maybe you
> want to handle a short int differently from a long double.

I can not understand why using explicit constructor will prevent such
issues. Could you provide some pseudo code please?


regards,
George

"Duane Hebert" wrote:

>
> "George" <George@discussions.microsoft.com> wrote in message
> news:5920D19C-EBD0-4EDD-BDA4-59B0E855BC29@microsoft.com...
> > Hi Bo,
> >
> >
> > I have finally find it. From the book, I only see how to use explicit
> > keyword -- its function. But I have not seen any practical scenarios why
> > we
> > should use explicit keyword. Any experience sharing from you are welcome.
> > :-)
> >
>
> Creating a ctor with the explicit keyword prevents it being used with an
> implicit cast. There are times when you don't want an implicit cast.
>
> Consider if you have multiple ctors that treat numbers. Maybe you
> want to handle a short int differently from a long double.
>
> Or maybe you want to prevent your ctor from being used with
> any but one type arg.
>
>
>

Re: explicit keyword by Duane

Duane
Tue Dec 11 04:23:16 PST 2007


"Duane Hebert" <spoo@flarn2.com> wrote in message
news:Of9Xj9%23OIHA.1188@TK2MSFTNGP04.phx.gbl...
>
> "George" <George@discussions.microsoft.com> wrote in message
> news:5920D19C-EBD0-4EDD-BDA4-59B0E855BC29@microsoft.com...
>> Hi Bo,
>>
>>
>> I have finally find it. From the book, I only see how to use explicit
>> keyword -- its function. But I have not seen any practical scenarios why
>> we
>> should use explicit keyword. Any experience sharing from you are welcome.
>> :-)
>>
>
> Creating a ctor with the explicit keyword prevents it being used with an
> implicit cast. There are times when you don't want an implicit cast.
>
> Consider if you have multiple ctors that treat numbers. Maybe you
> want to handle a short int differently from a long double.

That was a less than perfect example <g> Try googling. You
get stuff like:
http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx



Re: explicit keyword by Duane

Duane
Tue Dec 11 04:29:14 PST 2007


"George" <George@discussions.microsoft.com> wrote in message
news:FE5892F7-EA25-4BE4-8F73-2D82E9028D59@microsoft.com...
> Thanks Duane,
>
>
> I do not quite understand this point (and agree with other points).
>
>> Consider if you have multiple ctors that treat numbers. Maybe you
>> want to handle a short int differently from a long double.
>
> I can not understand why using explicit constructor will prevent such
> issues. Could you provide some pseudo code please?

That wasn't the best example. See the link with my subsequent post.

But do you understand implicit casting rules? Given different functions
(or constructors) with different args and calling them with an arg that
isn't explicitly correct, the compiler is going to try to find the closest
fit. Some time this is not what you want. Using the explicit keyword
for a ctor makes sure that no implicit conversions are attempted.




Re: explicit keyword by George

George
Tue Dec 11 04:51:01 PST 2007

Thanks ,


Good article, I have understood all points and agree. Except the following
one,

> presumably enough storage is being allocated for a new Array object, not to
> mention the memory reserved for 123 elements contained in the array. Surely
> this is not what the programmer intended!?

What is the storage issue?


regards,
George

"Duane Hebert" wrote:

>
> "Duane Hebert" <spoo@flarn2.com> wrote in message
> news:Of9Xj9%23OIHA.1188@TK2MSFTNGP04.phx.gbl...
> >
> > "George" <George@discussions.microsoft.com> wrote in message
> > news:5920D19C-EBD0-4EDD-BDA4-59B0E855BC29@microsoft.com...
> >> Hi Bo,
> >>
> >>
> >> I have finally find it. From the book, I only see how to use explicit
> >> keyword -- its function. But I have not seen any practical scenarios why
> >> we
> >> should use explicit keyword. Any experience sharing from you are welcome.
> >> :-)
> >>
> >
> > Creating a ctor with the explicit keyword prevents it being used with an
> > implicit cast. There are times when you don't want an implicit cast.
> >
> > Consider if you have multiple ctors that treat numbers. Maybe you
> > want to handle a short int differently from a long double.
>
> That was a less than perfect example <g> Try googling. You
> get stuff like:
> http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx
>
>
>

Re: explicit keyword by David

David
Tue Dec 11 04:50:16 PST 2007

George wrote:
> Thanks Duane,
>
>
> I do not quite understand this point (and agree with other points).
>
>> Consider if you have multiple ctors that treat numbers. Maybe you
>> want to handle a short int differently from a long double.
>
> I can not understand why using explicit constructor will prevent such
> issues. Could you provide some pseudo code please?

George:

Conversion operators (non-explicit constructors and cast operators)
weaken the strong type system of C++. They often prevent the compiler
from diagnosing errors.

--
David Wilkinson
Visual C++ MVP

Re: explicit keyword by George

George
Tue Dec 11 05:26:03 PST 2007

Cool, thanks David!


regards,
George

"David Wilkinson" wrote:

> George wrote:
> > Thanks Duane,
> >
> >
> > I do not quite understand this point (and agree with other points).
> >
> >> Consider if you have multiple ctors that treat numbers. Maybe you
> >> want to handle a short int differently from a long double.
> >
> > I can not understand why using explicit constructor will prevent such
> > issues. Could you provide some pseudo code please?
>
> George:
>
> Conversion operators (non-explicit constructors and cast operators)
> weaken the strong type system of C++. They often prevent the compiler
> from diagnosing errors.
>
> --
> David Wilkinson
> Visual C++ MVP
>

Re: explicit keyword by Duane

Duane
Tue Dec 11 12:49:59 PST 2007


"George" <George@discussions.microsoft.com> wrote in message
news:A14CD4CA-9E8A-4412-8E0C-B3DB96D7BA73@microsoft.com...
> Thanks ,
>
>
> Good article, I have understood all points and agree. Except the following
> one,
>
>> presumably enough storage is being allocated for a new Array object, not
>> to
>> mention the memory reserved for 123 elements contained in the array.
>> Surely
>> this is not what the programmer intended!?
>
> What is the storage issue?

The issue is that it's not clear that what is happening when the user
does UseArray(123) that he's acually doing UseArray(Array(123)).
Calling it with just the int seems like it's just sizing an array or
something,
not creating an instance as well.

If the ctor is explicit, this doesn't come up.



Re: explicit keyword by Duane

Duane
Tue Dec 11 16:08:28 PST 2007


"George" <George@discussions.microsoft.com> wrote in message
news:37F65D63-C8A4-4429-A50D-7FA7B364B78C@microsoft.com...
> Cool, thanks David!

>> Conversion operators (non-explicit constructors and cast operators)
>> weaken the strong type system of C++. They often prevent the compiler
>> from diagnosing errors.

David seems to have put this a lot more succint that I did.



Re: explicit keyword by George

George
Tue Dec 11 19:09:01 PST 2007

Thanks for your clarification, Duane!


regards,
George

"Duane Hebert" wrote:

>
> "George" <George@discussions.microsoft.com> wrote in message
> news:A14CD4CA-9E8A-4412-8E0C-B3DB96D7BA73@microsoft.com...
> > Thanks ,
> >
> >
> > Good article, I have understood all points and agree. Except the following
> > one,
> >
> >> presumably enough storage is being allocated for a new Array object, not
> >> to
> >> mention the memory reserved for 123 elements contained in the array.
> >> Surely
> >> this is not what the programmer intended!?
> >
> > What is the storage issue?
>
> The issue is that it's not clear that what is happening when the user
> does UseArray(123) that he's acually doing UseArray(Array(123)).
> Calling it with just the int seems like it's just sizing an array or
> something,
> not creating an instance as well.
>
> If the ctor is explicit, this doesn't come up.
>
>
>