https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94490
Bug ID: 94490 Summary: Ternary expression with 3 consts is “not” a constant expression Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: bisqwit at iki dot fi Target Milestone: --- Versions tested: GCC 9.3.0, GCC 10.0.1 20200324 (experimental) [master revision 596c90d3559:023579257f5:906b3eb9df6c577d3f6e9c3ea5c9d7e4d1e90536] Error message: test6.cc: In instantiation of ‘auto Mul(const T&, const T2&) [with T = std::array<float, 2>; T2 = std::array<float, 2>]’: test6.cc:20:58: required from here test6.cc:16:78: error: ‘(false ? std::tuple_size_v<std::array<float, 2> > : std::tuple_size_v<std::array<float, 2> >)’ is not a constant expression 16 | return typename arith_result<T,T2>::type { std::get<P>(vec) ... }; | ^ test6.cc:20:6: error: ‘void x’ has incomplete type 20 | auto x = Mul(std::array<float,2>{}, std::array<float,2>{}); | ^ Compiler is erroneously claiming that an expression of type (x ? y : z) where all of x,y,z are constant expressions, is not a constant expression. Code: #include <tuple> #include <functional> template<class T, class T2, std::size_t A=std::tuple_size_v<T>, std::size_t B=std::tuple_size_v<T>, std::size_t N = std::min(A,B), class S = std::make_index_sequence<(A<B)?A:B>> struct arith_result { using type = std::conditional_t<requires(T t){t[0]; }, decltype([]<std::size_t...I>(std::index_sequence<I...>) -> std::array<std::common_type_t<std::tuple_element_t<I,T>..., std::tuple_element_t<I,T2>...>, N>{}(S{})), decltype([]<std::size_t...I>(std::index_sequence<I...>) -> std::tuple<std::common_type_t<std::tuple_element_t<I,T>, std::tuple_element_t<I,T2>>...>{}(S{}))>; }; template<typename T = std::array<float,3>, typename T2 = T> auto Mul(const T& vec, const T2& val) { return [&]<std::size_t... P>(std::index_sequence<P...>) { return typename arith_result<T,T2>::type { std::get<P>(vec) ... }; } (std::make_index_sequence<2>{}); } auto x = Mul(std::array<float,2>{}, std::array<float,2>{}); Note that if I replace the Mul function with this (inline the lambda call), the problem goes away: template<typename T = std::array<float,3>, typename T2 = T> auto Mul(const T& vec, const T2& val) { return typename arith_result<T,T2>::type { std::get<0>(vec),std::get<1>(vec) }; } Somehow the compiler forgets to do constant folding while it is processing the lambda.