"词汇"意味着它与源代码有关.
例如,1是一个词法常量.OTOH,sizeof(char)也是一个编译时积分常量表达式,但它不是一个词法常量.从词汇上讲,它是对sizeof运营商的一种调用.
词法运算符处理源代码.预处理器运算符属于此类别.
在大多数情况下,无论我使用1还是sizeof(char)在我的程序中的任何地方都没有区别.但是,正如词汇运算符的论点#或##它产生了相当大的差异,因为这些对实际代码起作用而不是评估结果:
#define STR(x) #x
std::string one = STR(1);
std::string also_one = STR(sizeof(char));
Run Code Online (Sandbox Code Playgroud)
最后,词法范围是指程序源代码中存在标识符的部分(可以使用).这与动态范围(也称为对象生存期)形成对比,动态范围是对象存在的程序的一部分(维护其值并且可以通过指针或引用间接操作,即使名称不在词法范围内) .
string f(string x) { return "2" + x; } // main's "y" is not in lexical scope, however it is in dynamic scope, and will not be destroyed yet
int main(void)
{
string y = "5.2"; // y enters lexical scope and dynamic scope
string z = f("y"); // y leaves lexical scope as f is called, and comes back into lexical scope when f returns
return z.size();
// z leaves lexical and dynamic scope, destructor is called
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1510 次 |
| 最近记录: |