gpt4 book ai didi

c++ - typedef 模板,这个可行吗?

转载 作者:行者123 更新时间:2023-11-30 01:44:10 25 4
gpt4 key购买 nike

是否可以这样做:

template< typename T >
using T_CCUnit = static const constexpr T;

事实上,我只是想要一个 static const constexpr 的别名,因为每次都在某个文件中编写真的很无聊。我更喜欢 c++11 中的解决方案。

最佳答案

您可以很容易地向类型添加 const-ness,因为 cv-qualifiers 修改了类型:

template <typename T>
using T_C = std::add_const_t<T>;

然而,staticconstexpr修改声明,而不是类型。所以,在这里:

                         int x1;
const int x2;
static const int x3;
static constexpr const int x4;
// | ^ | type |name
// ^ | constexpr specifier
// storage class specifier

最后三个声明的类型都是const int ,但是x2具有与 x3 不同的存储类别和 x4 .

老实说,如果主要问题在于

... it's really boring to write ...

我最好的建议是您学习如何在您喜欢的编辑器中编写宏或保存常用代码片段。

关于c++ - typedef 模板,这个可行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36625503/

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