C ++ usando Copia-CTOR quando viene utilizzato Operator= () - Esattamente come funziona?
-
21-12-2019 - |
Domanda
Quali sono esattamente le regole per C ++ conversione di un assegnatore di operatore= () a una costruzione?Come Foo foo = bar
chiamerà effettivamente il costruttore di Foo che accetta la barra come argomento, se esiste.Ho googlato per come funziona ma non riesco a trovare nulla.
Sto avendo un problema Scaricando il motivo per cui l'incarico di seguito sta cercando di prendere un costruttore ma non prendendolo ovviamente corretto: manubrio (tipo e risorsa).La costruzione che utilizza la sintassi della costruzione effettiva funziona bene, ma non con l'operatore di assegnazione.
Codice (ovviamente modificato per Brevity):
template< typename TYPE >
class HandlePtr {
public:
HandlePtr( void ) = default;
HandlePtr( HandlePtr< TYPE >& other ) = default;
HandlePtr( TYPE& resource ) {} // generally I would make this explicit, but for testing purposes I took it out
~HandlePtr( void ) = default;
public:
HandlePtr<TYPE>& operator=( TYPE& resource ) { return *this; }
HandlePtr<TYPE>& operator=( HandlePtr<TYPE>& other ) { return *this; }
};
int main ( void ) {
int x = 5;
HandlePtr< int > g( x ); // works
HandlePtr< int > i;i = x; // works
HandlePtr< int > h = x; // doesn't work
// also tried this just out of curiosity:
HandlePtr< int > h = HandlePtr< int >( x ); // also does not work
return 0;
}
.
Errori:
shit.cpp: In function ‘int main()’:
try.cpp:19:24: error: no matching function for call to ‘HandlePtr<int>::HandlePtr(HandlePtr<int>)’
HandlePtr< int > h = x; // doesn't work
^
try.cpp:19:24: note: candidates are:
try.cpp:7:3: note: HandlePtr<TYPE>::HandlePtr(TYPE&) [with TYPE = int]
HandlePtr( TYPE& resource ) {} // generally I would make this explicit, but for testing purposes I took it out
^
try.cpp:7:3: note: no known conversion for argument 1 from ‘HandlePtr<int>’ to ‘int&’
try.cpp:6:3: note: HandlePtr<TYPE>::HandlePtr(HandlePtr<TYPE>&) [with TYPE = int]
HandlePtr( HandlePtr< TYPE >& other ) = default;
^
try.cpp:6:3: note: no known conversion for argument 1 from ‘HandlePtr<int>’ to ‘HandlePtr<int>&’
try.cpp:5:3: note: HandlePtr<TYPE>::HandlePtr() [with TYPE = int]
HandlePtr( void ) = default;
^
try.cpp:5:3: note: candidate expects 0 arguments, 1 provided
try.cpp:20:20: error: redeclaration of ‘HandlePtr<int> h’
HandlePtr< int > h = HandlePtr< int >( x ); // also does not work
^
try.cpp:19:20: error: ‘HandlePtr<int> h’ previously declared here
HandlePtr< int > h = x; // doesn't work
. Soluzione
Ti stai trascurando con il Dichiarazione :
T t = u;
.
Questo non è l'operatore di assegnazione. t = u;
non è una sotto-espressione di una dichiarazione. L'unica espressione qui è u
; E il risultato della valutazione dell'espressione u
viene utilizzato come inizializzatore per l'oggetto t
viene dichiarato.
Se u
ha tipo T
, quindi t
è costruito in copia da u
.
Se u
non ha il tipo T
, quindi u
deve essere prima convertito in Tipo T
. Questo crea un rvalue di tipo T
.
Non si dispone di costruttori che accettano un rvalue, quindi T t = u;
e il T t = T(u);
identico non funziona entrambi. Tuttavia, T t(u)
riesce perché non viene creato alcun rvalue; Il valore u
viene utilizzato come argomento al T(U &)
del costruttore.
Esempio di codice semplificato:
struct T
{
T(int &);
T(T&);
T();
T &operator=(int &);
};
int main()
{
int x = 5;
T g(x); // OK, T(int &)
T g2(5); // fail, looks for T(int const &)
T i; // OK, T()
i = x; // OK, T::operator=(int&)
T h3 = i; // OK, T(T&)
T h1 = T(x); // fail, looks for T(T const &)
T h2 = x; // fail, identical to previous line
}
.
Normalmente è necessario utilizzare const &
come parametro per i costruttori di copia e gli operatori di assegnazione; Quindi tutti questi casi "falliscono" diventano "ok", poiché un rvalue può essere vincolato a un riferimento const.