class A
{
public:

A(int)
{
}

private:

A(const A &);
A &operator=(const A &);
};

A foo()
{
return A(0);
}

void bar()
{
A a = foo();
}

Even though the compiler may optimize out the call to the copy constructor,
it should still respect the fact that it is declared private.

From the C++ standard:

"Even when the creation of the temporary object is avoided, all the semantic
restrictions must be respected as if the temporary object was created."

Re: Why does this compile with VC++? by Victor

Victor
Mon Feb 26 15:07:19 CST 2007

Defect Architect wrote:
> class A
> {
> public:
>
> A(int)
> {
> }
>
> private:
>
> A(const A &);
> A &operator=(const A &);
> };
>
> A foo()
> {
> return A(0);
> }
>
> void bar()
> {
> A a = foo();
> }
>
> Even though the compiler may optimize out the call to the copy
> constructor, it should still respect the fact that it is declared
> private.
>
> From the C++ standard:
>
> "Even when the creation of the temporary object is avoided, all the
> semantic restrictions must be respected as if the temporary object
> was created."

1>------ Build started: Project: test, Configuration: Release Win32 ------
1>Compiling...
1>Test.cpp
1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
declared in class 'A'
1> .\Test.cpp(11) : see declaration of 'A::A'
1> .\Test.cpp(2) : see declaration of 'A'
1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
declared in class 'A'
1> .\Test.cpp(11) : see declaration of 'A::A'
1> .\Test.cpp(2) : see declaration of 'A'
1> while checking that elided copy-constructor 'A::A(const A &)' is
callable
1> .\Test.cpp(11) : see declaration of 'A::A'
1> when converting from 'A' to 'const A &'
1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
declared in class 'A'
1> .\Test.cpp(11) : see declaration of 'A::A'
1> .\Test.cpp(2) : see declaration of 'A'
1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
declared in class 'A'
1> .\Test.cpp(11) : see declaration of 'A::A'
1> .\Test.cpp(2) : see declaration of 'A'
1> while checking that elided copy-constructor 'A::A(const A &)' is
callable
1> .\Test.cpp(11) : see declaration of 'A::A'
1> when converting from 'A' to 'const A &'
1>test - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Not sure what you're complaining about.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Re: Why does this compile with VC++? by DefectArchitect

DefectArchitect
Mon Feb 26 15:29:15 CST 2007

"Victor Bazarov" wrote:

> Defect Architect wrote:
> > class A
> > {
> > public:
> >
> > A(int)
> > {
> > }
> >
> > private:
> >
> > A(const A &);
> > A &operator=(const A &);
> > };
> >
> > A foo()
> > {
> > return A(0);
> > }
> >
> > void bar()
> > {
> > A a = foo();
> > }
> >
> > Even though the compiler may optimize out the call to the copy
> > constructor, it should still respect the fact that it is declared
> > private.
> >
> > From the C++ standard:
> >
> > "Even when the creation of the temporary object is avoided, all the
> > semantic restrictions must be respected as if the temporary object
> > was created."
>
> 1>------ Build started: Project: test, Configuration: Release Win32 ------
> 1>Compiling...
> 1>Test.cpp
> 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
> declared in class 'A'
> 1> .\Test.cpp(11) : see declaration of 'A::A'
> 1> .\Test.cpp(2) : see declaration of 'A'
> 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
> declared in class 'A'
> 1> .\Test.cpp(11) : see declaration of 'A::A'
> 1> .\Test.cpp(2) : see declaration of 'A'
> 1> while checking that elided copy-constructor 'A::A(const A &)' is
> callable
> 1> .\Test.cpp(11) : see declaration of 'A::A'
> 1> when converting from 'A' to 'const A &'
> 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
> declared in class 'A'
> 1> .\Test.cpp(11) : see declaration of 'A::A'
> 1> .\Test.cpp(2) : see declaration of 'A'
> 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
> declared in class 'A'
> 1> .\Test.cpp(11) : see declaration of 'A::A'
> 1> .\Test.cpp(2) : see declaration of 'A'
> 1> while checking that elided copy-constructor 'A::A(const A &)' is
> callable
> 1> .\Test.cpp(11) : see declaration of 'A::A'
> 1> when converting from 'A' to 'const A &'
> 1>test - 4 error(s), 0 warning(s)
> ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
>
> Not sure what you're complaining about.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask
>
>
>

Sorry. That did not accurately reproduce my scenario. What about this:

class A
{
public:

A(int)
{
}

private:

A(const A &);
A &operator=(const A &);
};

class B : public A
{
public:
B(int) : A(0)
{
}
};

B foo()
{
return 0;
}

void bar()
{
B b = foo();
}

Does B have an implicit copy constructor? If so, how does it handle A's
private copy constructor? If it works, why can I not add this line to bar() ?

B b2 = b;

Thanks!

Re: Why does this compile with VC++? by Victor

Victor
Mon Feb 26 16:02:16 CST 2007

Defect Architect wrote:
> "Victor Bazarov" wrote:
>
>> Defect Architect wrote:
>>> class A
>>> {
>>> public:
>>>
>>> A(int)
>>> {
>>> }
>>>
>>> private:
>>>
>>> A(const A &);
>>> A &operator=(const A &);
>>> };
>>>
>>> A foo()
>>> {
>>> return A(0);
>>> }
>>>
>>> void bar()
>>> {
>>> A a = foo();
>>> }
>>>
>>> Even though the compiler may optimize out the call to the copy
>>> constructor, it should still respect the fact that it is declared
>>> private.
>>>
>>> From the C++ standard:
>>>
>>> "Even when the creation of the temporary object is avoided, all the
>>> semantic restrictions must be respected as if the temporary object
>>> was created."
>>
>> 1>------ Build started: Project: test, Configuration: Release Win32
>> ------ 1>Compiling...
>> 1>Test.cpp
>> 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
>> declared in class 'A'
>> 1> .\Test.cpp(11) : see declaration of 'A::A'
>> 1> .\Test.cpp(2) : see declaration of 'A'
>> 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
>> declared in class 'A'
>> 1> .\Test.cpp(11) : see declaration of 'A::A'
>> 1> .\Test.cpp(2) : see declaration of 'A'
>> 1> while checking that elided copy-constructor 'A::A(const A
>> &)' is callable
>> 1> .\Test.cpp(11) : see declaration of 'A::A'
>> 1> when converting from 'A' to 'const A &'
>> 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
>> declared in class 'A'
>> 1> .\Test.cpp(11) : see declaration of 'A::A'
>> 1> .\Test.cpp(2) : see declaration of 'A'
>> 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
>> declared in class 'A'
>> 1> .\Test.cpp(11) : see declaration of 'A::A'
>> 1> .\Test.cpp(2) : see declaration of 'A'
>> 1> while checking that elided copy-constructor 'A::A(const A
>> &)' is callable
>> 1> .\Test.cpp(11) : see declaration of 'A::A'
>> 1> when converting from 'A' to 'const A &'
>> 1>test - 4 error(s), 0 warning(s)
>> ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
>> ==========
>>
>> Not sure what you're complaining about.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask
>>
>>
>>
>
> Sorry. That did not accurately reproduce my scenario. What about
> this:
>
> class A
> {
> public:
>
> A(int)
> {
> }
>
> private:
>
> A(const A &);
> A &operator=(const A &);
> };
>
> class B : public A
> {
> public:
> B(int) : A(0)
> {
> }
> };
>
> B foo()
> {
> return 0;
> }
>
> void bar()
> {
> B b = foo();
> }
>
> Does B have an implicit copy constructor? If so, how does it handle
> A's private copy constructor? If it works, why can I not add this
> line to bar() ?
>
> B b2 = b;

Looks like a bug. I can also see the same behaviour in Comeau C++
(using online test drive). I am going to ask Comeau about, and will
report back when I have something from them.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Re: Why does this compile with VC++? by DefectArchitect

DefectArchitect
Mon Feb 26 16:10:05 CST 2007

"Defect Architect" wrote:

> "Victor Bazarov" wrote:
>
> > Defect Architect wrote:
> > > class A
> > > {
> > > public:
> > >
> > > A(int)
> > > {
> > > }
> > >
> > > private:
> > >
> > > A(const A &);
> > > A &operator=(const A &);
> > > };
> > >
> > > A foo()
> > > {
> > > return A(0);
> > > }
> > >
> > > void bar()
> > > {
> > > A a = foo();
> > > }
> > >
> > > Even though the compiler may optimize out the call to the copy
> > > constructor, it should still respect the fact that it is declared
> > > private.
> > >
> > > From the C++ standard:
> > >
> > > "Even when the creation of the temporary object is avoided, all the
> > > semantic restrictions must be respected as if the temporary object
> > > was created."
> >
> > 1>------ Build started: Project: test, Configuration: Release Win32 ------
> > 1>Compiling...
> > 1>Test.cpp
> > 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
> > declared in class 'A'
> > 1> .\Test.cpp(11) : see declaration of 'A::A'
> > 1> .\Test.cpp(2) : see declaration of 'A'
> > 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
> > declared in class 'A'
> > 1> .\Test.cpp(11) : see declaration of 'A::A'
> > 1> .\Test.cpp(2) : see declaration of 'A'
> > 1> while checking that elided copy-constructor 'A::A(const A &)' is
> > callable
> > 1> .\Test.cpp(11) : see declaration of 'A::A'
> > 1> when converting from 'A' to 'const A &'
> > 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
> > declared in class 'A'
> > 1> .\Test.cpp(11) : see declaration of 'A::A'
> > 1> .\Test.cpp(2) : see declaration of 'A'
> > 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
> > declared in class 'A'
> > 1> .\Test.cpp(11) : see declaration of 'A::A'
> > 1> .\Test.cpp(2) : see declaration of 'A'
> > 1> while checking that elided copy-constructor 'A::A(const A &)' is
> > callable
> > 1> .\Test.cpp(11) : see declaration of 'A::A'
> > 1> when converting from 'A' to 'const A &'
> > 1>test - 4 error(s), 0 warning(s)
> > ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
> >
> > Not sure what you're complaining about.
> >
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask
> >
> >
> >
>
> Sorry. That did not accurately reproduce my scenario. What about this:
>
> class A
> {
> public:
>
> A(int)
> {
> }
>
> private:
>
> A(const A &);
> A &operator=(const A &);
> };
>
> class B : public A
> {
> public:
> B(int) : A(0)
> {
> }
> };
>
> B foo()
> {
> return 0;
> }
>
> void bar()
> {
> B b = foo();
> }
>
> Does B have an implicit copy constructor? If so, how does it handle A's
> private copy constructor? If it works, why can I not add this line to bar() ?
>
> B b2 = b;
>
> Thanks!

This does not compile with gcc 4.02.

Re: Why does this compile with VC++? by Duane

Duane
Tue Feb 27 20:00:12 CST 2007


"Defect Architect" <DefectArchitect@discussions.microsoft.com> wrote in
message news:03DC3017-A427-4D46-9109-351A9886866A@microsoft.com...
> "Defect Architect" wrote:
>
>> "Victor Bazarov" wrote:
>>
>> > Defect Architect wrote:
>> > > class A
>> > > {
>> > > public:
>> > >
>> > > A(int)
>> > > {
>> > > }
>> > >
>> > > private:
>> > >
>> > > A(const A &);
>> > > A &operator=(const A &);
>> > > };
>> > >
>> > > A foo()
>> > > {
>> > > return A(0);
>> > > }
>> > >
>> > > void bar()
>> > > {
>> > > A a = foo();
>> > > }
>> > >
>> > > Even though the compiler may optimize out the call to the copy
>> > > constructor, it should still respect the fact that it is declared
>> > > private.
>> > >
>> > > From the C++ standard:
>> > >
>> > > "Even when the creation of the temporary object is avoided, all the
>> > > semantic restrictions must be respected as if the temporary object
>> > > was created."
>> >
>> > 1>------ Build started: Project: test, Configuration: Release
>> > Win32 ------
>> > 1>Compiling...
>> > 1>Test.cpp
>> > 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
>> > declared in class 'A'
>> > 1> .\Test.cpp(11) : see declaration of 'A::A'
>> > 1> .\Test.cpp(2) : see declaration of 'A'
>> > 1>.\Test.cpp(17) : error C2248: 'A::A' : cannot access private member
>> > declared in class 'A'
>> > 1> .\Test.cpp(11) : see declaration of 'A::A'
>> > 1> .\Test.cpp(2) : see declaration of 'A'
>> > 1> while checking that elided copy-constructor 'A::A(const A &)'
>> > is
>> > callable
>> > 1> .\Test.cpp(11) : see declaration of 'A::A'
>> > 1> when converting from 'A' to 'const A &'
>> > 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
>> > declared in class 'A'
>> > 1> .\Test.cpp(11) : see declaration of 'A::A'
>> > 1> .\Test.cpp(2) : see declaration of 'A'
>> > 1>.\Test.cpp(22) : error C2248: 'A::A' : cannot access private member
>> > declared in class 'A'
>> > 1> .\Test.cpp(11) : see declaration of 'A::A'
>> > 1> .\Test.cpp(2) : see declaration of 'A'
>> > 1> while checking that elided copy-constructor 'A::A(const A &)'
>> > is
>> > callable
>> > 1> .\Test.cpp(11) : see declaration of 'A::A'
>> > 1> when converting from 'A' to 'const A &'
>> > 1>test - 4 error(s), 0 warning(s)
>> > ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
>> > ==========
>> >
>> > Not sure what you're complaining about.
>> >
>> > V
>> > --
>> > Please remove capital 'A's when replying by e-mail
>> > I do not respond to top-posted replies, please don't ask
>> >
>> >
>> >
>>
>> Sorry. That did not accurately reproduce my scenario. What about this:
>>
>> class A
>> {
>> public:
>>
>> A(int)
>> {
>> }
>>
>> private:
>>
>> A(const A &);
>> A &operator=(const A &);
>> };
>>
>> class B : public A
>> {
>> public:
>> B(int) : A(0)
>> {
>> }
>> };
>>
>> B foo()
>> {
>> return 0;
>> }
>>
>> void bar()
>> {
>> B b = foo();
>> }
>>
>> Does B have an implicit copy constructor? If so, how does it handle A's
>> private copy constructor? If it works, why can I not add this line to
>> bar() ?
>>
>> B b2 = b;
>>
>> Thanks!
>
> This does not compile with gcc 4.02.

Borland Turbo C++ generates an error:
[C++ Error] Unit1.cpp(34): E2285 Could not find a match for 'B::B(const B&)'
on the line B b = foo();




Re: Why does this compile with VC++? by Victor

Victor
Wed Feb 28 07:26:00 CST 2007

Duane Hebert wrote:
> "Defect Architect" <DefectArchitect@discussions.microsoft.com> wrote
> in message news:03DC3017-A427-4D46-9109-351A9886866A@microsoft.com...
>> "Defect Architect" wrote:
>>> [..] What about
>>> this: class A
>>> {
>>> public:
>>>
>>> A(int)
>>> {
>>> }
>>>
>>> private:
>>>
>>> A(const A &);
>>> A &operator=(const A &);
>>> };
>>>
>>> class B : public A
>>> {
>>> public:
>>> B(int) : A(0)
>>> {
>>> }
>>> };
>>>
>>> B foo()
>>> {
>>> return 0;
>>> }
>>>
>>> void bar()
>>> {
>>> B b = foo();
>>> }
>>>
>>> Does B have an implicit copy constructor? If so, how does it
>>> handle A's private copy constructor? If it works, why can I not
>>> add this line to bar() ?
>>>
>>> B b2 = b;
>>>
>>> Thanks!
>>
>> This does not compile with gcc 4.02.
>
> Borland Turbo C++ generates an error:
> [C++ Error] Unit1.cpp(34): E2285 Could not find a match for
> 'B::B(const B&)' on the line B b = foo();

I've not received the answer from Comeau Computing yet, but the
consensus in comp.lang.c++[.moderated] is that it's a bug in Comeau
(and in Visual C++, obviously). The program is ill-formed because
the copy constructor cannot be generated for 'B' and "B b = foo();"
does require for the copy constructor to exist under the "as if the
copy is actually made" rule.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Re: Why does this compile with VC++? by Duane

Duane
Wed Feb 28 10:59:51 CST 2007


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:es3vt9$mcn$1@news.datemas.de...
> >> This does not compile with gcc 4.02.
> >
> > Borland Turbo C++ generates an error:
> > [C++ Error] Unit1.cpp(34): E2285 Could not find a match for
> > 'B::B(const B&)' on the line B b = foo();
>
> I've not received the answer from Comeau Computing yet, but the
> consensus in comp.lang.c++[.moderated] is that it's a bug in Comeau
> (and in Visual C++, obviously). The program is ill-formed because
> the copy constructor cannot be generated for 'B' and "B b = foo();"
> does require for the copy constructor to exist under the "as if the
> copy is actually made" rule.

Yep. I just thought it rare that Borland seems to get it right
when Comeau and MS get it wrong <g>