public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression
@ 2020-04-05  2:18 bisqwit at iki dot fi
  2020-04-07 22:39 ` [Bug c++/94490] " mpolacek at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: bisqwit at iki dot fi @ 2020-04-05  2:18 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-12-02 13:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
2020-04-07 22:39 ` [Bug c++/94490] " mpolacek at gcc dot gnu.org
2020-04-08 13:43 ` ensadc at mailnesia dot com
2021-05-27 19:15 ` ppalka at gcc dot gnu.org
2021-05-27 21:03 ` mpolacek at gcc dot gnu.org
2021-12-01 17:23 ` mpolacek at gcc dot gnu.org
2021-12-02 13:21 ` cvs-commit at gcc dot gnu.org
2021-12-02 13:26 ` cvs-commit at gcc dot gnu.org
2021-12-02 13:28 ` mpolacek at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).