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: