2 c++ reference decltype rvalue-reference c++11
很简单的问题,我无法用谷歌搜索出答案。
例如:
int a = 0;
int& b = x;
int&& c = 1;
decltype((a)) x; // what is the type of x?
decltype((b)) y; // what is the type of y?
decltype((c)) z; // what is the type of z?
Run Code Online (Sandbox Code Playgroud)
我不确定我应该为x,y和z分配一些值以获得不同的结果。
编辑:
根据双括号下面的站点将示例int转换为参考:https :
//github.com/AnthonyCalandra/modern-cpp-features#decltype
int a = 1; // `a` is declared as type `int`
int&& f = 1; // `f` is declared as type `int&&`
decltype(f) g = 1; // `decltype(f) is `int&&`
decltype((a)) h = g; // `decltype((a))` is int&
Run Code Online (Sandbox Code Playgroud)
根据C++ Primer:
当我们应用
decltype到一个没有任何括号的变量时,我们得到该变量的类型。如果我们将变量的名称包装在一组或多组括号中,编译器会将操作数计算为表达式。变量是可以作为赋值左侧的表达式。结果,decltype在这样的表达式上产生了一个引用:Run Code Online (Sandbox Code Playgroud)// decltype of a parenthesized variable is always a reference decltype((i)) d; // error: d is int& and must be initialized decltype(i) e; // ok: e is an (uninitialized) int
| 归档时间: |
|
| 查看次数: |
142 次 |
| 最近记录: |