【发布时间】:2018-12-16 14:59:54
【问题描述】:
当我编译以下程序时(https://wandbox.org/permlink/fl6yrLYI2sRrZJHP)
#include <iostream>
using std::cout;
using std::endl;
template <typename...> class WhichType;
int main() {
const auto& integer = 0;
[integer]() {
WhichType<decltype(integer)>{};
}();
}
为什么编译器会说decltype(integer) 是const int&?我的理解是这应该是const int 和decltype((integer)) 应该已经解析为const int&。
换句话说,我的理解是 lambda 会解析为这样的结构
struct Lambda {
public:
explicit Lambda(const int& integer_) : integer{integer_} {}
void operator()() const {
WhichType<decltype(integer)>{};
}
const int integer;
};
为什么decltype(integer) 解析为引用类型?这只是decltype 表现特殊的另一种情况吗?类似于结构化绑定中引用类型和实际类型的区别?
【问题讨论】:
-
Visual C++ 2017 报告
const int。 MinGW g++ 7.3.0 报告const int&.
标签: c++ lambda language-lawyer c++17 decltype