Dlaczego to ostrzeżenie z kompilatora IBM XL C / C ++?
Oto minimalny przykład kodu ilustrujący problem:
#include <iostream>
class Thing
{
// Non-copyable
Thing(const Thing&);
Thing& operator=(const Thing&);
int n_;
public:
Thing(int n) : n_(n) {}
int getValue() const { return n_;}
};
void show(const Thing& t)
{
std::cout << t.getValue() << std::endl;
}
int main()
{
show(3);
}
Daje to ten sam błąd:
int main()
{
show( Thing(3) );
}
Kompilator IBM XL C / C ++ 8.0 w systemie AIX wysyła następujące ostrzeżenia:
"testWarning.cpp", line 24.9: 1540-0306 (W) The "private" copy constructor "Thing(const Thing &)" cannot be accessed.
"testWarning.cpp", line 24.9: 1540-0308 (I) The semantics specify that a temporary object must be constructed.
"testWarning.cpp", line 24.9: 1540-0309 (I) The temporary is not constructed, but the copy constructor must be accessible.
Próbowałem także g ++ 4.1.2 z „-Wall” i „-pedantic” i nie otrzymałem żadnej diagnostyki. Dlaczego wymagany jest tutaj dostęp do konstruktora kopii? W jaki sposób mogę wyeliminować ostrzeżenie, poza tym, że obiekt może być kopiowany (co jest poza moją kontrolą) lub sprawia, że kopia jest jawna (jeśli rzeczywisty obiekt jest drogi w kopiowaniu)?