decltypeを理解してコンパイル時に式の型を判別しようとしています。私は二重の変数でそれを行うのは、例えばましょう:左辺値のdecltype括弧構文
#include <iostream>
#include <type_traits>
int main(){
double a;
typedef decltype(a) a_type;
typedef decltype((a)) ref_a_type;
typedef decltype(a)& o_ref_a_type;
a_type b;
ref_a_type c = b;
o_ref_a_type d = b;
if (std::is_same<decltype(b), double>::value) std::cout << "b is double\n";
if (std::is_same<decltype(c), double&>::value) std::cout << "c is double&\n";
if (std::is_same<decltype(d), double&>::value) std::cout << "d is double&\n";
}
私が正しく理解している場合は、これらの点が真でなければなりません:
decltype(a)
戻りdouble&
a
場合はそうでない場合は左辺値とdouble
です。- decltypeは、変数に適用されない限り、式の型を推論します。この場合、変数の型が推測されます。
- 変数がカッコで囲まれている場合、変数は左辺式になります。
したがって、decltype((a))
とdecltype(a)&
はa
が可変でない場合、例えば、この場合には等価であるが、必ずしも等価(
typedef decltype((5)) ref_a_type;
typedef decltype(5)& o_ref_a_type;
次いで、両方のタイプが等しくないref_a_type
はint
とo_ref_a_type
はint&
です。この場合、余分な括弧は役に立たないためです)。誰かがこれのより良い説明を与えることができますか?私は最初か二番目の方法を使うべきですか? IMOよりも読みやすく理解しやすいようです。 decltype(e)
ため
'T'が非参照型の場合、' T f(); '、' T & f(); '、' T & f(); 'の' decltype(f) 'は' T'、 'T&'、 'T &&' 'T && f();' –