c++ - reference_wrapper and implicit conversion -


considering following code:

int v = 12; std::reference_wrapper<int> x(v); std::reference_wrapper<const int> y(x);  // (1) works std::reference_wrapper<const int> z = x; // (2) fails 

if understand correctly, (1) works because requires single user-conversion, (2) fails because involves 2 user-conversions in conversion sequence:

std::reference_wrapper<int>::operator int& , std::reference_wrapper<const int>::(const int&) 

thus, std::reference_wrapper<int> not implicitly convertible std::reference_wrapper<const int> breaks of code use std::is_convertible trait.

is there reason design involving non-presence of generic copy constructor:

template <typename y> reference_wrapper<t>::reference_wrapper(const reference_wrapper<y>&) 

(just in std::shared_ptr example) allow such implicit conversion ?

there no implicit autoconversion between std::reference_wrapper , underlying type (since can unsafe , confusing).

the correct syntax should be:

std::reference_wrapper<const int> z = x.get(); 

Comments