2
다음 코드 결과 0100 (CLang, GNU ++ 14 준수). func는 rvalue 벡터를 매개 변수로 취하고 이후 forward (c) [0]은 int의 const 참조이므로 decltype (자동)의 유형 공제는 const int &이되어야합니다. 결과를 이해하도록 도와주세요. 감사!rvalue int 벡터의 요소에서 decltype (자동)의 유형 공제가 int & gt; 인 이유는 무엇입니까?
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