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

            Bug ID: 93901
           Summary: noexcept specifier on ctor does not work with
                    constexpr variable or expression
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: paul.groke at dynatrace dot com
  Target Milestone: ---

Seems like with GCC 10, only the literal `true` works as expected in noexcept
specifiers for ctors. Other expressions that evaluate to "true" result in ctors
that test as "noexcept(false)". Noexcept specifiers on functions seem to work
just fine though.
Example:


#include <type_traits>
#include <new>

extern void *mem;

constexpr bool YES = true;

struct NoexceptTrueCtor {
        NoexceptTrueCtor() noexcept(true);
};
void NoexceptTrueFun() noexcept(true);

struct NoexceptYesCtor {
        NoexceptYesCtor() noexcept(YES);
};
void NoexceptYesFun() noexcept(YES);

struct NoexceptOneEqOneCtor {
        NoexceptOneEqOneCtor() noexcept(1 == 1);
};
void NoexceptOneEqOneFun() noexcept(1 == 1);

struct NoNoexceptCtor {
        NoNoexceptCtor();
};
void NoNoexceptFun();

static_assert(std::is_nothrow_default_constructible<NoexceptTrueCtor>::value,
"1"); // OK
static_assert(noexcept(new(mem) NoexceptTrueCtor), "2"); // OK
static_assert(noexcept(NoexceptTrueFun()), "3"); // OK

static_assert(std::is_nothrow_default_constructible<NoexceptYesCtor>::value,
"4"); // fail
static_assert(noexcept(new(mem) NoexceptYesCtor), "5"); // fail
static_assert(noexcept(NoexceptYesFun()), "6"); // OK

static_assert(std::is_nothrow_default_constructible<NoexceptOneEqOneCtor>::value,
"7"); // fail
static_assert(noexcept(new(mem) NoexceptOneEqOneCtor), "8"); // fail
static_assert(noexcept(NoexceptOneEqOneFun()), "9"); // OK

static_assert(!std::is_nothrow_default_constructible<NoNoexceptCtor>::value,
"10"); // OK
static_assert(!noexcept(new(mem) NoNoexceptCtor), "11"); // OK
static_assert(!noexcept(NoNoexceptFun()), "12"); // OK


This fails with


<source>:32:71: error: static assertion failed: 4
   32 |
static_assert(std::is_nothrow_default_constructible<NoexceptYesCtor>::value,
"4"); // fail
      |              
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
<source>:33:15: error: static assertion failed: 5
   33 | static_assert(noexcept(new(mem) NoexceptYesCtor), "5"); // fail
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:36:76: error: static assertion failed: 7
   36 |
static_assert(std::is_nothrow_default_constructible<NoexceptOneEqOneCtor>::value,
"7"); // fail
      |              
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
<source>:37:15: error: static assertion failed: 8
   37 | static_assert(noexcept(new(mem) NoexceptOneEqOneCtor), "8"); // fail
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1


Tested with godbolt.org: https://godbolt.org/z/BvXe4K
x86-64, g++ (Compiler-Explorer-Build) 10.0.1 20200223 (experimental)

GCC versions 9.2 and older compile the above code just fine as do Clang, MSVC
and ICC.

Reply via email to