Kiedy konieczne jest użycie std :: ref?
Rozważać:
std::tuple<int , const A&> func (const A& a)
{
return std::make_tuple( 0 , std::ref(a) );
}
Jeststd::ref
wymagane do napisania poprawnego i przenośnego kodu? (Bez tego dobrze się kompiluje)
Tło:
Jeśli usunęstd::ref
mój kod działa dobrze bez żadnych ostrzeżeń (g++-4.6 -Wall
), ale nie działa poprawnie.
W przypadku zainteresowania definicjaA
:
struct A {
std::array<int,2> vec;
typedef int type_t;
template<typename... OPs,typename... VALs>
A& operator=(const std::pair< std::tuple<VALs...> , std::tuple<OPs...> >& e) {
for( int i = 0 ; i < vec.size() ; ++i ) {
vec[i] = eval( extract(i,e.first) , e.second );
}
}
};