जब हम एक static_assertमें उपयोग करना चाहते हैं तो हमें if constexprकुछ टेम्पलेट पैरामीटर पर निर्भर होना चाहिए। दिलचस्प बात यह है कि जब कोड लैम्बडा में लपेटा जाता है तो जीसीसी और क्लैग असहमत होते हैं।
निम्न कोड gcc के साथ संकलित करता है, लेकिन क्लैग मुखर को ट्रिगर करता है, भले ही वह if constexprसच न हो।
#include <utility>
template<typename T> constexpr std::false_type False;
template<typename T>
void foo() {
auto f = [](auto x) {
constexpr int val = decltype(x)::value;
if constexpr(val < 0) {
static_assert(False<T>, "AAA");
}
};
f(std::integral_constant<int, 1>{});
}
int main() {
foo<int>();
}
इसे आसानी से प्रतिस्थापित False<T>करके तय किया जा सकता है False<decltype(x)>।
तो सवाल यह है कि कौन सा संकलक सही है? मुझे लगता है कि जीसीसी सही है क्योंकि हालत static_assertपर निर्भर है T, लेकिन मुझे यकीन नहीं है।
static_assert(False<int>, "AAA");के static_assert(false, "AAA");अंदर बराबर है ।
f(std::integral_constant<int, 1>{});Wandbox मुखर ट्रिगर नहीं करता है: wandbox.org/permlink/UFYAmYwtt1ptsndr