c++ - 可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型吗
问题描述
// header
int extern has_a_type; // (1) extern declaration
// implementation
decltype(has_a_type) // (2) unnecessarily verbose type inference code
has_a_type; // (3) definition
我知道我可以使用decltype
,所以在定义(3)(并可能初始化)它时,我实际上不必键入(甚至在某种程度上知道)extern 声明的(1)变量的类型。然而decltype
,迫使我写出变量的名称(可能是完全限定的和长的)两次(2)。
我怎样才能避免写两次?类似的东西auto has_a_type;
(当然,这行不通)。
解决方案
你不能——开玩笑,因为没有人说服标准委员会相信能够写作的好处
int extern has_a_type;
auto has_a_type;
尽管它易于处理。结果可能是
decltype(auto) has_a_type;
decltype(has_a_type)
为了消除初始化器的类型推导歧义,这是必要的,然后,不幸的是,我们与已经可用的重复性相距不远。
推荐阅读
- c# - 如何使用使用 C# .NET Core REST API 中的库的客户端重命名 JsonPropertyName
- powerbi - Power BI 矩阵总计不会加起来
- asp.net-core-3.1 - ModelState 验证,一次只显示一个错误
- javascript - 当 iOS Swift 应用程序发送到后台时,“无法设置标题后发送到客户端”NodeJS 错误
- list - 将分解的时间序列矩阵转换为 R 中的向量列表?
- ktor - Ktor - gradle 测试任务失败
- javascript - 如何使用 javascript 在具有多个表的树中找到最大 td 单元格?
- r - 在 ifelse() 语句内部与外部运行一行时的不同输出
- google-calendar-api - 插入“指定时间范围为空”时出现 Google 日历 API 错误
- spring-batch - 如何在 Spring Batch 中定义“java.lang.String”类型的 bean?