Consider the following code:
#include<memory>
struct A {
std::auto_ptr<int> i;
};
A F() {
A a;
return a;
}
int main(int argc, char **argv) {
A a = F();
return 0;
}
When compiling I receive a compilation error, (see here):
error: no matching function for call to ‘A::A(A)’
A a = F();
^
To my understanding, A::A(A)
isn't even allowed to exist, so why is the compiler requesting it? Secondly, why is it not using RVO?
If it is because a std::auto_ptr
cannot be returned from a function, why does the following compile and run?
#include<memory>
std::auto_ptr<int> F() {
std::auto_ptr<int> ap;
return ap;
}
int main(int argc, char **argv) {
std::auto_ptr<int> ap = F();
return 0;
}
I cannot use C++11 in my current work unfortunately, hence the use of auto_ptr
.