Topic: explicit contructors


Author: loufoque@remove.gmail.com (loufoque)
Date: Fri, 21 Jul 2006 23:03:34 GMT
Raw View
uvts_cvs@yahoo.com wrote :

> Declaring this constructor explicit forbids something like:
>
> foo bar(0); // Allowed
> bar = 1; // Forbidden line!

This line calls operator=, not the constructor.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jdennett@acm.org (James Dennett)
Date: Sat, 22 Jul 2006 16:26:20 GMT
Raw View
loufoque wrote:
> uvts_cvs@yahoo.com wrote :
>
>> Declaring this constructor explicit forbids something like:
>>
>> foo bar(0); // Allowed
>> bar = 1; // Forbidden line!
>
> This line calls operator=, not the constructor.

It calls operator=, and in some circumstances would call
a converting constructor -- the latter being the point here,
as an explicit constructor is *not* a converting constructor.

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: uvts_cvs@yahoo.com
Date: Wed, 19 Jul 2006 10:06:54 CST
Raw View
Hi, everybody.

Consider this tiny class.

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

Declaring this constructor explicit forbids something like:

foo bar(0); // Allowed
bar = 1; // Forbidden line!

But it also forbids:
foo bar = 0;

Can anybody explain me why we need to forbid this line when "foo
bar(0);" is allowed. Aren't they equivalent 'cause they both mean
"construct a foo object starting from 1 just calling the specific
constructor foo::foo(int)"?

Thanks, Daniele.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: petebecker@acm.org (Pete Becker)
Date: Wed, 19 Jul 2006 15:34:18 GMT
Raw View
uvts_cvs@yahoo.com wrote:
> Hi, everybody.
>
> Consider this tiny class.
>
> struct foo
> {
>    explicit foo(int){}
> };
>
> Declaring this constructor explicit forbids something like:
>
> foo bar(0); // Allowed
> bar = 1; // Forbidden line!
>
> But it also forbids:
> foo bar = 0;
>
> Can anybody explain me why we need to forbid this line when "foo
> bar(0);" is allowed. Aren't they equivalent 'cause they both mean
> "construct a foo object starting from 1 just calling the specific
> constructor foo::foo(int)"?
>

foo bar = 0; means construct a temporary object of type foo initialized
with the value 0, and use the copy constructor to initialize bar with
that temporary object. Since the construction of the temporary object is
not allowed, the statement isn't valid.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Wed, 19 Jul 2006 16:51:49 CST
Raw View
Pete Becker wrote:
> uvts_cvs@yahoo.com wrote:
> > Hi, everybody.
> >
> > Consider this tiny class.
> >
> > struct foo
> > {
> >    explicit foo(int){}
> > };
> >
> > Declaring this constructor explicit forbids something like:
> >
> > foo bar(0); // Allowed
> > bar = 1; // Forbidden line!
> >
> > But it also forbids:
> > foo bar = 0;
> >
> > Can anybody explain me why we need to forbid this line when "foo
> > bar(0);" is allowed. Aren't they equivalent 'cause they both mean
> > "construct a foo object starting from 1 just calling the specific
> > constructor foo::foo(int)"?
> >
>
> foo bar = 0; means construct a temporary object of type foo initialized
> with the value 0, and use the copy constructor to initialize bar with
> that temporary object. Since the construction of the temporary object is
> not allowed, the statement isn't valid.

Since bar has a constructor that accepts an int parameter, there is
never any temporary object involved in foo's construction. The original
poster is correct in the thesis that:

       foo bar(0);

is for all practical purposes just another way of expressing:

       foo bar = 0;

And the only reason that the choice of syntax used to construct bar
matters to bar's explicit constructor is because    12.3.1/2 states that
it must. (And this rule is arbitrary, the Standard could just have
easily made the latter syntax acceptable and the former syntax,
ill-formed.)

The more difficult question to answer - and the one the original poster
actually seems to be asking - is why do explicit constructors
distinguish between alternate forms of the same expression. My own
guess is - that since the "explicit" keyword is meant to limit the
opportunities for an object's construction, it makes sense to limit as
well the number of different syntaxes that construct the object to the
minimum number needed: one.

Greg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: Wed, 19 Jul 2006 22:39:45 GMT
Raw View
Greg Herlihy wrote:
> Pete Becker wrote:
>
>>uvts_cvs@yahoo.com wrote:
>>>Consider this tiny class.
>>>
>>>struct foo
>>>{
>>>   explicit foo(int){}
>>>};
>>>
>>>Declaring this constructor explicit forbids something like:
>>>
>>>foo bar(0); // Allowed
>>>bar = 1; // Forbidden line!
>>>
>>>But it also forbids:
>>>foo bar = 0;
>>>
>>>Can anybody explain me why we need to forbid this line when "foo
>>>bar(0);" is allowed. Aren't they equivalent 'cause they both mean
>>>"construct a foo object starting from 1 just calling the specific
>>>constructor foo::foo(int)"?
>>>
>>
>>foo bar = 0; means construct a temporary object of type foo initialized
>>with the value 0, and use the copy constructor to initialize bar with
>>that temporary object. Since the construction of the temporary object is
>>not allowed, the statement isn't valid.
>
>
> Since bar has a constructor that accepts an int parameter, there is
> never any temporary object involved in foo's construction.

Not so. Refer to C++ Standard section 8.5, paragraphs 11 onward.

> The original poster is correct in the thesis that:
>
>        foo bar(0);
>
> is for all practical purposes just another way of expressing:
>
>        foo bar = 0;

No, they are not the same, although in common cases they have the same effect.
One case where they are very different is when the constructor is explicit.

The statement
 foo bar(0);  // direct initialization
means construct a foo object "bar" using 0 as the initializer.

As Pete explained, the statement
 foo bar = 0; // copy-initialization
means convert 0 to a foo, and initialize object "bar" via the copy constructor.
Converting 0 to a foo involves an implicit conversion via a foo constructor.
When the constructor is explicit, implicit conversions are not allowed. (C++
Standard section 12.3.1.)

The statement
 foo bar = foo(0);
is equivalent to
 foo bar = 0;
except that it is valid when the foo constructor is explicit -- the value 0 is
explicitly converted to a foo.

---
Steve Clamage

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: petebecker@acm.org (Pete Becker)
Date: Thu, 20 Jul 2006 05:08:06 GMT
Raw View
Greg Herlihy wrote:

>
> Since bar has a constructor that accepts an int parameter, there is
> never any temporary object involved in foo's construction. The original
> poster is correct in the thesis that:
>
>        foo bar(0);
>
> is for all practical purposes just another way of expressing:
>
>        foo bar = 0;
>

I suggest you read 8.5/15. The two are not equivalent.

> The more difficult question to answer - and the one the original poster
> actually seems to be asking - is why do explicit constructors
> distinguish between alternate forms of the same expression.

It's not at all difficult. 8.5/15 describes the two different mechanisms
that these two forms of initialization use. Once you understand the
mechanics of that second statement, it's clear how the restriction falls
out of the rules.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: uvts_cvs@yahoo.com
Date: Thu, 20 Jul 2006 09:38:30 CST
Raw View
Greg Herlihy wrote:

> The more difficult question to answer - and the one the original poster
> actually seems to be asking - is why do explicit constructors
> distinguish between alternate forms of the same expression. My own
> guess is - that since the "explicit" keyword is meant to limit the
> opportunities for an object's construction, it makes sense to limit as
> well the number of different syntaxes that construct the object to the
> minimum number needed: one.

That's the point. My question is: 'Why do they coded in the standard
that the two expression s are different?', and not 'Reading the
standard I cannot understand why the two expressions are not the same
thing. Can you explain it?'.

Daniele.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]