[expr.ref]/1中提到的脚注的确切含义是什么? [英] What exactly is the meaning of the footnote mentioned in [expr.ref]/1?

查看:137
本文介绍了[expr.ref]/1中提到的脚注的确切含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[expr.ref]/1 :

后缀表达式,后跟点.或箭头->,并可选地后面跟关键字template(17.2), 后跟 id-expression 的是后缀表达式.点或箭头前的后缀表达式 进行评估; 67 评估的结果与 id-expression 一起确定 整个后缀表达式.

A postfix expression followed by a dot . or an arrow ->, optionally followed by the keyword template (17.2), and then followed by an id-expression, is a postfix expression. The postfix expression before the dot or arrow is evaluated;67 the result of that evaluation, together with the id-expression, determines the result of the entire postfix expression.

67)如果评估了类成员访问表达式,则即使结果对于 确定整个后缀表达式的值,例如,如果 id-expression 表示静态成员.

67) If the class member access expression is evaluated, the subexpression evaluation happens even if the result is unnecessary to determine the value of the entire postfix expression, for example if the id-expression denotes a static member.

推荐答案

如果将某个成员定义为static,则该成员对于该类只有一个副本,对于该类的每个实例都没有一个副本.静态成员可以通过类的实例(对象)进行引用.该脚注说明,即使您不需要知道要访问哪个实例对象就知道静态成员的值,也可以对标识该实例的表达式进行求值(并且会发生任何副作用).

If a member is defined as static, then there is only one copy of that member for the class, not one copy for each instance of the class. Static members can be referenced via an instance (an object) of the class. The footnote clarifies that the expression identifying the instance is evaluated (and any side effects take place) even though you don't need to know which instance object you're accessing to know the value of the static member.

一个例子:

#include <iostream>

class foo {
    public:
        static int s;
};

int foo::s = 42;

int index() {
    std::cout << "index returns 5\n";
    return 5;
}

int main() {
    foo arr[10];
    std::cout << arr[index()].s << "\n";
}

只有一个s对象,其值是42,但是即使不需要确定结果s的值,表达式arr[index()]仍将被求值.

There's only one s object, and its value is 42, but the expression arr[index()] is still evaluated, even though its result isn't needed to determine the value of s.

输出为:

index returns 5
42

这篇关于[expr.ref]/1中提到的脚注的确切含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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