gpt4 book ai didi

c++ - 如何获取 C/C++ 变量的属性

转载 作者:太空狗 更新时间:2023-10-29 23:30:58 25 4
gpt4 key购买 nike

如何获取变量的属性?

例子:

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?

我只读过有关改变变量常数的内容,但没有读过其他内容。

最佳答案

我很确定您可以为 const 使用模板元编程和 volatile . 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';
}

打印

false
true

还有 std::is_volatile<>

关于c++ - 如何获取 C/C++ 变量的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com