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