[Bug c++/63924] New: Constexpr constructible expression "is not constexpr" when used in a template non-type argument

mouchtaris at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Nov 18 01:09:00 GMT 2014


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63924

            Bug ID: 63924
           Summary: Constexpr constructible expression "is not constexpr"
                    when used in a template non-type argument
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mouchtaris at gmail dot com

A proper constexpr constructible type is found non-constexpr by the compiler,
under the following conditions:

- it is used in an expression which is passed as a template non-type argument,
- its copy constructor is defaulted (whether explicitly or implicitly).

In the following test case this is demostrated:

// -------------------------------------------------------
// File t.cpp
// -------------------------------------------------------
// utils
template <unsigned N> struct require_constexpr {
  static constexpr unsigned value = N;
};
template <typename...> constexpr void noop (void) { }

// a constexpr constructible class
struct test { 
  constexpr unsigned size() const { return 0; }
  constexpr test() { }
  constexpr test(const test &) = default;
};

// size wrappers
constexpr auto size0 (test t) { return t.size(); }
// just making sure type "test" is still considered
constexpr auto size1 (test t) { return size0(t); }
// constexpr constructible outside a template argument
constexpr auto size2 (test t) { return size1(t); }
// PROBLEM here
constexpr auto size3 (test t) { return require_constexpr< size0(t) >::value; }

int main (int, char**)
{
  constexpr auto const ar = test { };

  noop<
    require_constexpr< size3(ar) >
  >();

  return 0;
}
// -------------------------------------------------------


Compiling with
    g++ -std=c++1y -pedantic -Wall -Wextra -o /tmp/a t.cpp 

and here is the output:
//--------------------------------------------------------
t.cpp: In function ‘constexpr auto size3(test)’:
t.cpp:22:68: error: ‘t’ is not a constant expression
 constexpr auto size3 (test t) { return require_constexpr< size0(t) >::value; }
                                                                    ^
t.cpp:22:68: note: in template argument for type ‘unsigned int’ 
t.cpp:22:16: error: invalid return type ‘void’ of constexpr function ‘constexpr
auto size3(test)’
 constexpr auto size3 (test t) { return require_constexpr< size0(t) >::value; }
                ^
t.cpp: In function ‘int main(int, char**)’:
t.cpp:29:34: error: could not convert template argument ‘size3((ar, test()))’
to ‘unsigned int’
     require_constexpr< size3(ar) >
                                  ^
t.cpp:30:5: error: no matching function for call to ‘noop()’
   >();
     ^
t.cpp:30:5: note: candidate is:
t.cpp:6:39: note: template<class ...> constexpr void noop()
 template <typename...> constexpr void noop (void) { }
                                       ^
t.cpp:6:39: note:   template argument deduction/substitution failed:
t.cpp:30:5: error: template argument 1 is invalid
   >();
     ^
//--------------------------------------------------------

"test" is still properly constexpr-copy-constructed when used outside
template arguments, as in size1() and size2().

The code segment also compiles cleanly if we define a non-default constructor.
In other words, if we change line
  constexpr test(const test &) = default;
to
  constexpr test(const test &) { }

So the bug (probably) pertrains to defaulting constructors and the use of such
in non-type template arguments.


More information about the Gcc-bugs mailing list