public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bisqwit at iki dot fi" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression
Date: Sun, 05 Apr 2020 02:18:20 +0000	[thread overview]
Message-ID: <bug-94490-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2020-04-05  2:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-05  2:18 bisqwit at iki dot fi [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-94490-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).