这段代码在g++ (coliru)下编译正常,但在MSVC (godbolt和我的VS2017)下无法编译。
#include <type_traits>
#include <iostream>
template<class T> void f(){
constexpr bool b=std::is_same_v<T,int>; //#1
auto func_x=[&](){
if constexpr(b){ //#error
}else{
}
};
func_x();
}
int main(){
f<int>();
}
(6): error C2131: 表达式未评估为常量
(6): 注意:失败原因是在其生命周期外读取变量
(6): 注意:见'this'的使用
哪个编译器(g++ 或 MSVC)错了?
在“见'this'的使用”中,“this”是什么?
如何解决此问题,同时保持编译时保证?
在我的实际情况中,b (#1)
是一个依赖于其他几个constexpr变量的复杂语句。