如何获得C/C++变量的属性

hus*_*sik 2 c c++ variables scope

如何获取变量的属性?

例:

int a = 5;
....
....
isConstant(a); //Prints "no!" if 'a' is not a constant at this time.
isRegister(a); //Prints "yes!" if 'a' is a register at this time.important.
isVolatile(a); //Prints "trusted" if 'a' is volatile.
isLocal(a);    //If it is temporary.
isStatic(a);   //Static?
Run Code Online (Sandbox Code Playgroud)

我只读过关于改变变量的常量但不改变其他变量的常量.

Luc*_*ore 7

我很确定你可以使用模板元编程constvolatile.IDK约register,我很确定你不能用于static本地范围的变量.

例如,在C++ 11中,您有:

#include <iostream>
#include <type_traits>

int main() 
{
    std::cout << boolalpha;
    std::cout << std::is_const<int>::value << '\n';
    std::cout << std::is_const<const int>::value  << '\n';
}
Run Code Online (Sandbox Code Playgroud)

版画

false
true
Run Code Online (Sandbox Code Playgroud)

还有 std::is_volatile<>