2014-06-24 5 views
5

Gli oggetti all'interno dell'oggetto con valore di riferimento valgono anche i valori di riferimento?Gli oggetti all'interno dell'oggetto con valore di riferimento valgono anche i valori di riferimento?

struct A{ 
}; 

struct B{ 
    A a2; 
}; 

//template<class B> 
void test(B &&b){ 

    // 1. Is this the correct way? 
    auto &&in3 = std::forward<B>(b).a2; 
    std::cout << std::is_rvalue_reference<decltype(in3)>::value; 
    // return true 

    // 2. or this? 
    auto &&in4 = b.a2; 
    std::cout << std::is_rvalue_reference<decltype(in4)>::value;  
    // return false   
} 

test(B()); 

http://coliru.stacked-crooked.com/a/bcf0f7dc4cc0440e

risposta

7

Sì, i membri del rvalues ​​sono essi stessi rvalues. Questo è stato chiarito dalla DR 421

Ma questo è irrilevante qui:

auto & & in4 = b.a2;

b non è un rvalore, è un lvalue (semplice regola empirica: ha un nome).

Per ripristinare la categoria valore che aveva quando passata alla funzione è necessario forward esso

+1

Il problema sembra essere chiariti ulteriormente [qui] (http://open-std.org/jtc1/sc22/wg21 /docs/cwg_defects.html#616). –