2
以下代碼結果0100(符合CLang,GNU ++ 14)。我希望0001,因爲需要FUNC右值向量作爲參數,然後向前(C)[0]是爲int const引用,所以鍵入decltype(自動)的扣應導致到const int的&。請幫我理解結果。謝謝!爲什麼從rvalue的int元素類型中扣除decltype(auto)是int&?
template <typename T>
decltype(auto) func(T&& c)
{
return forward<T>(c)[0];
}
int main(int argc, const char * argv[])
{
cout
<< is_same< int, decltype(func(vector<int>{3}))>::value
<< is_same< int&, decltype(func(vector<int>{3}))>::value
<< is_same< const int, decltype(func(vector<int>{3}))>::value
<< is_same< const int&, decltype(func(vector<int>{3}))>::value
<< endl;
return 0;
}
輸出:
0100
'std2'?從來沒有聽說過。你有沒有參考指出?謝謝。另外,我想這個問題是關於'decltype(auto)'你得到實際類型的事實,即'U'代表'operator []'而不是'U'。 – skypjack
@skypjack編譯器實際返回'int&',OP期望'const int&'。 –
@ A.S.H哦,我明白了。錯過了問題中的評論。抱歉。 – skypjack