c++ - 使用变量的类型作为模板参数

标签 c++ c++11 types c++14 c++17

我做了以下事情:

long long int x = 0;
int digits_of_x = std::numeric_limits<long long int>::digits;

而且效果很好。但是,如果有人更改了 x 的类型,这很容易引入错误。所以我更愿意这样做:

long long int x = 0;
int digits_of_x = std::numeric_limits<typeof(x)>::digits;

我找到了 GCC 扩展 typeof() 但我想使用一些标准函数(最高为 C++17)。有没有这个功能?

最佳答案

您想使用decltype,而不是typeof。自 C++11 起可用:

long long int x = 0;
int digits_of_x = std::numeric_limits<decltype(x)>::digits;

关于c++ - 使用变量的类型作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53383927/

相关文章:

c++ - 离散傅立叶变换的实现 - FFT

c++ - 用于 move std::shared_ptr 的函数声明

c++ - 具有返回抽象类型的函数头是否合法?

.net - 如何检查IEnumerable是否为列表?

C++ Visual Studio 2012 -> 2010

C++ STL 列表大小

c++ - 在 win32 richedit 中更改文本颜色

c++ - 错误 C2893 : Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'

java - 对多种数据类型使用相同的函数

sql - 了解从 bytea 到 oid 的转换