括号在decltype((c))中的意义? [英] Significance of parentheses in decltype((c))?

查看:802
本文介绍了括号在decltype((c))中的意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读维基百科上关于 C ++ 11 的文章

有一个例子,我引用:

#include <vector>
int main() {  
    const std::vector<int> v(1);  
    auto a = v[0];        // a has type int  
    decltype(v[1]) b = 1; // b has type const int&, the return type of  
                          //   std::vector<int>::operator[](size_type) const  
    auto c = 0;           // c has type int  
    auto d = c;           // d has type int  
    decltype(c) e;        // e has type int, the type of the entity named by c  
    decltype((c)) f = c;  // f has type int&, because (c) is an lvalue  
    decltype(0) g;        // g has type int, because 0 is an rvalue  
}

    decltype(c) e;        // e has type int, the type of the entity named by c  
    decltype((c)) f = c;  // f has type int&, because (c) is an lvalue  

c (c)?为什么(c)表示左值

推荐答案


  • c 是变量的名称;

    (c)是一个表达式,在这种情况下是一个 lvalue 表达式,其值与变量 c

    (c) is an expression, in this case an lvalue expression, whose value is identical to the value of the variable c.

    c> decltype 。例如,考虑 decltype(1 + 2),这也是一个采用表达式的例子。它只是这样发生,你的例子是一个简单版本的表达式:一个只命名一个变量,并没有什么令人兴奋的。

    And the two are treated differently by decltype. Consider, for example, decltype(1+2), which is also an example taking an expression. It just so happens that your example is a simple version of an expression: one which merely names a single variable and does nothing exciting with it.

    这是其中一个差异,你通常只真正关心,如果你合理化的语言规范的微妙部分;

    It's one of those differences that you generally only really care about if you're rationalising about the subtle parts of the language specification; though, as you have identified, it has quite a significant practical effect in this case.

    请注意,这里没有操作符的用法。 。这一切都是从语法的布局中扣除的。

    Please note though that there is no operator usage here. It's all simply a deduction from the layout of the grammar.

    这篇关于括号在decltype((c))中的意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆