Topic: Conversion-Constructor


Author: Frank Uepping <Frank.Uepping@epost.de>
Date: Mon, 25 Feb 2002 03:02:44 GMT
Raw View
//Look at this:

class A {};

class B {
public:
        B(const A&) {}
private:
        B(const B&);
};


B b1(A());
//I assume this form of initialization is compliant to the standard, isn't it?


B b2 = A();
//But what about this?
//A clever compiler might turn it into the first form implicitly!


Regards
    FAU


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Mon, 25 Feb 2002 11:16:17 GMT
Raw View
On Mon, 25 Feb 2002 03:02:44 GMT, Frank Uepping <Frank.Uepping@epost.de>
wrote:

> class A {};
> class B {
> public:
>         B(const A&) {}
> private:
>         B(const B&);
> };


> B b1(A());
> //I assume this form of initialization is compliant to the standard, isn't it?

That declares a function, B b1 (A (*f)()), but

A a1;
B b1(a1);

is a valid initialization.

> B b2 = A();
> //But what about this?
> //A clever compiler might turn it into the first form implicitly!

It might, but it would be wrong without a diagnostic.  The semantics
require an accessible copy ctor.  The implementation may optimize the
use away but not the semantics.

John

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: Mon, 25 Feb 2002 11:37:33 GMT
Raw View
"Frank Uepping" <Frank.Uepping@epost.de> wrote in message
news:3C792670.DF712B6B@epost.de...
> class A {};
>
> class B {
> public:
>         B(const A&) {}
> private:
>         B(const B&);
> };
>
>
> B b1(A());
> //I assume this form of initialization is compliant to the standard, isn't
it?

This declares a function b1 that accepts an unnamed argument of type
function-returning-A and returns an object of type B.

B b1((A())); // note the extra ()

must be used to disambiguate the definition of an object from the
declaration of a function. Alternatively, create a named A object and use
that

A aTemp;
B b1(aTemp);

> B b2 = A();
> //But what about this?
> //A clever compiler might turn it into the first form implicitly!

No. This is _copy_ initialization, and is equivalent to

B b2(B(A()));

in other words, the copy-constructor must be accessible (which it isn't, as
it is private). It may not get _called_ if it is visible (as the compiler is
free to eliminate the temporary in this case) but that is another issue.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer



---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Craig Tiller" <ctiller@bigpond.net.au>
Date: Tue, 26 Feb 2002 17:55:56 GMT
Raw View
"Frank Uepping" <Frank.Uepping@epost.de> wrote in message
news:3C792670.DF712B6B@epost.de...
> //Look at this:
>
> class A {};
>
> class B {
> public:
>         B(const A&) {}
> private:
>         B(const B&);
> };
>
>
> B b1(A());
> //I assume this form of initialization is compliant to the standard, isn't
it?
>
>
> B b2 = A();
> //But what about this?
> //A clever compiler might turn it into the first form implicitly!
>
>

The compiler has to turn it into the first form implicitly as things are - B
b2 = A() is just a hold over from C days to write B b2(A()) - its not a
"real" assignment

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Tue, 26 Feb 2002 17:55:45 GMT
Raw View
Frank Uepping <Frank.Uepping@epost.de> writes:

| //Look at this:
|
| class A {};
|
| class B {
| public:
|         B(const A&) {}
| private:
|         B(const B&);
| };
|
|
| B b1(A());
| //I assume this form of initialization is compliant to the standard, isn't it?

It is.

| B b2 = A();
| //But what about this?

Invalid.

| //A clever compiler might turn it into the first form implicitly!

Only a buggy compiler will do that. Because, it is required to first
check the accessibility of the copy-constructor before applying any
form of optimisation.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Tue, 26 Feb 2002 17:56:27 GMT
Raw View
In article <3C792670.DF712B6B@epost.de>, Frank Uepping
<Frank.Uepping@epost.de> writes
>//Look at this:
>
>class A {};
>
>class B {
>public:
>        B(const A&) {}
>private:
>        B(const B&);
>};
>
>
>B b1(A());
>//I assume this form of initialization is compliant to the standard, isn't it?
>
>
>B b2 = A();
>//But what about this?
>//A clever compiler might turn it into the first form implicitly!

Indeed it might attempt to do that, but it would have to check for an
accessible copy ctor first, and as written it would fail. The second
form is only equivalent to the first if the intialiser is already the
same type as the object being declared, if not a conversion must take
place (in theory). The compiler must check that it has 1) an accessible
conversion (it has, in this case your public ctor for B) and an
accessible copy ctor (which it does not have in this case)


--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Tue, 26 Feb 2002 18:27:40 GMT
Raw View
Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> writes:

[...]

| | B b1(A());
| | //I assume this form of initialization is compliant to the standard, isn't it?
|
| It is.

Following up to my own message.

I confused the above (which is a function declaration) with an
initialisation.  Sorry for the noise.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "frank_lee" <frank_lee99@hotmail.com>
Date: Tue, 26 Feb 2002 18:47:22 GMT
Raw View
"John Potter" <jpotter@falcon.lhup.edu> wrote in message
news:3c79fa89.73667725@news.earthlink.net...
> On Mon, 25 Feb 2002 03:02:44 GMT, Frank Uepping <Frank.Uepping@epost.de>
> wrote:
>
> > class A {};
> > class B {
> > public:
> >         B(const A&) {}
> > private:
> >         B(const B&);
> > };
>
>
> > B b1(A());
> > //I assume this form of initialization is compliant to the standard,
isn't it?
>
> That declares a function, B b1 (A (*f)()), but
B b1(A());
works as a definition on Sun and VC++
>
> A a1;
> B b1(a1);
>
> is a valid initialization.
>
> > B b2 = A();
> > //But what about this?
> > //A clever compiler might turn it into the first form implicitly!
>
> It might, but it would be wrong without a diagnostic.  The semantics
> require an accessible copy ctor.  The implementation may optimize the
> use away but not the semantics.
>
> John
>
> ---
> [ 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.research.att.com/~austern/csc/faq.html                ]
>

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Wed, 27 Feb 2002 03:47:48 GMT
Raw View
On Tue, 26 Feb 2002 18:47:22 GMT, "frank_lee" <frank_lee99@hotmail.com>
wrote:

> "John Potter" <jpotter@falcon.lhup.edu> wrote in message
> news:3c79fa89.73667725@news.earthlink.net...

> > On Mon, 25 Feb 2002 03:02:44 GMT, Frank Uepping <Frank.Uepping@epost.de>
> > wrote:

> > > class A {};
> > > class B {
> > > public:
> > >         B(const A&) {}
> > > private:
> > >         B(const B&);
> > > };

> > > B b1(A());
> > > //I assume this form of initialization is compliant to the standard,
> > > isn't it?

> > That declares a function, B b1 (A (*f)())

> B b1(A());
> works as a definition on Sun and VC++

Oh?  I doubt that.  What does it define and how did you use it?

b1 = b1; // Is this accepted?  It should not be.

A g();  // The function.
b1(g);  // Is this accepted?  It should be.

John

---
[ 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.research.att.com/~austern/csc/faq.html                ]