Программы на C++. Форум разработчиков
Anonymous
Почему это нельзя оценить во время компиляции?
Сообщение
Anonymous » 05 сен 2025, 23:19
Это компилируется как на Clang, так и на GCC, но терпит неудачу, когда я не насыщен линии, и мне интересно, почему с тех пор .set () метод на std :: bitset - это константспр. < /p>
Код: Выделить всё
#include
#include
struct Bitset
{
std::bitset defined_ids;
template
bool ContainsAnyOf(property_ids ... ids) const
{
using bitset_type_t = decltype(defined_ids);
auto getBitset = [&] () consteval -> bitset_type_t
{
bitset_type_t result;
//(result.set(ids), ...); // THIS LINE FAILS
//error: constexpr variable 'compare_bitset' must be initialized by a constant expression
return result;
};
constexpr bitset_type_t zero_bitset;
constexpr bitset_type_t compare_bitset = getBitset();
return (compare_bitset & defined_ids) != zero_bitset;
}
};
int main()
{
constexpr Bitset bitset;
bitset.ContainsAnyOf(1, 2, 3, 4);
return 0;
}
< /code>
Вот ссылка Godbolt на вышеупомянутое. GCC - единственный, что компилируется, даже если я расстроен метод .set Call: < /p>
#include
#include
struct Bitset
{
std::bitset defined_ids;
template
bool ContainsAnyOf() const
{
using bitset_type_t = decltype(defined_ids);
auto getBitset = [] () consteval -> bitset_type_t
{
bitset_type_t result;
//(result.set(ids), ...);
// Clang doesn't compile this at all
// GCC and MSVC compile even with this line uncommented.
return result;
};
constexpr bitset_type_t zero_bitset;
constexpr bitset_type_t compare_bitset = getBitset.template operator()();
return (compare_bitset & defined_ids) != zero_bitset;
}
};
int main()
{
constexpr Bitset bitset;
bitset.ContainsAnyOf();
return 0;
}
ссылка на вторую версию.>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... mpile-time
1757103585
Anonymous
Это компилируется как на Clang, так и на GCC, но терпит неудачу, когда я не насыщен линии, и мне интересно, почему с тех пор .set () метод на std :: bitset - это константспр. < /p> [code]#include #include struct Bitset { std::bitset defined_ids; template bool ContainsAnyOf(property_ids ... ids) const { using bitset_type_t = decltype(defined_ids); auto getBitset = [&] () consteval -> bitset_type_t { bitset_type_t result; //(result.set(ids), ...); // THIS LINE FAILS //error: constexpr variable 'compare_bitset' must be initialized by a constant expression return result; }; constexpr bitset_type_t zero_bitset; constexpr bitset_type_t compare_bitset = getBitset(); return (compare_bitset & defined_ids) != zero_bitset; } }; int main() { constexpr Bitset bitset; bitset.ContainsAnyOf(1, 2, 3, 4); return 0; } < /code> Вот ссылка Godbolt на вышеупомянутое. GCC - единственный, что компилируется, даже если я расстроен метод .set Call: < /p> #include #include struct Bitset { std::bitset defined_ids; template bool ContainsAnyOf() const { using bitset_type_t = decltype(defined_ids); auto getBitset = [] () consteval -> bitset_type_t { bitset_type_t result; //(result.set(ids), ...); // Clang doesn't compile this at all // GCC and MSVC compile even with this line uncommented. return result; }; constexpr bitset_type_t zero_bitset; constexpr bitset_type_t compare_bitset = getBitset.template operator()(); return (compare_bitset & defined_ids) != zero_bitset; } }; int main() { constexpr Bitset bitset; bitset.ContainsAnyOf(); return 0; } [/code] ссылка на вторую версию.> Подробнее здесь: [url]https://stackoverflow.com/questions/79757117/why-cant-this-be-evaluated-at-compile-time[/url]