2

I understand that the title of the question is confusing, so here is the example:

#include<array>
#include<optional>
#include<type_traits>

static constexpr std::optional<int> maybe_int() {
    return  {64};
}
struct s {

    using type = std::conditional_t< maybe_int().has_value(), std::array<int, *maybe_int()>,float > ;
};
static_assert(std::is_same_v<s::type, std::array<int,64>>);

This code compiles fine but when I move maybe_int inside struct compilation fails.

error: 'static constexpr std::optional<int> s::maybe_int()' called in a constant expression before its definition is complete

Note that I know the code works fine with free function, this is about readability. So I would like to move maybe_int inside struct s.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • Which compiler are you using? [GCC compiles as-is](https://godbolt.org/g/s8EjHq). – Xirema Jun 11 '18 at 15:54
  • 2
    @Xirema like I said : when I move maybe_int inside struct compilation fails – NoSenseEtAl Jun 11 '18 at 15:56
  • 4
    @Xirema Are you being serious? Is is *that* hard to move the function *exactly* the way the OP specified? –  Jun 11 '18 at 15:57
  • 1
    @Xirema the question stated "This code compiles fine." [This is the code that causes the failure](https://godbolt.org/g/sBdhGT). – Drew Dormann Jun 11 '18 at 16:00
  • You won't be able to due to the reason in the answer on the duplicate. You could put the code in a namespace so as to not pollute the global space. – NathanOliver Jun 11 '18 at 16:04
  • 1
    @Rakete1111 FWIW, there's [an open issue](http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1626) suggesting that the template case should be handled the same as the non-template case, at which point your suggestion would stop working. –  Jun 11 '18 at 16:07
  • @hvd Ha, I knew this was hack :) Thanks for mentioning this, I didn't know. – Rakete1111 Jun 11 '18 at 16:08

0 Answers0