考虑以下代码。如果我的理解if constexpr
是正确的,则else
不应编译该分支,因此z()
不应将其视为错误。
#include <type_traits>
struct Z{};
template<typename T>
void f(T z) {
auto lam = [z]() {
if constexpr(std::is_same<T, Z>::value) {
} else {
z();
}
};
}
int main() {
f(Z{});
}
在clang和gcc中编译;但对于最新的 MSVC,它不会。不幸的是,goldbolt 的 MSVC 太旧了,但在我的机器上完全更新了 VS 2017,cl /std:c++17
产生:
Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26428.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
if_constexpr.cpp
if_constexpr.cpp(10): error C2064: term does not evaluate to a function taking 0 arguments
if_constexpr.cpp(16): note: see reference to function template instantiation 'void f<Z>(T)' being compiled
with
[
T=Z
]
如果移除了封闭的 lambda,则代码将在所有三个编译器上编译。
我做错了什么或不受支持,还是只是一个 MSVC 错误?