Bug 92411

Summary: conformance issue with reinterpret_cast in constant expressions
Product: gcc Reporter: Darrell Wright <Darrell.Wright>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: mpolacek, webrown.cpp, wjwray
Priority: P3 Keywords: accepts-invalid
Version: unknown   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82304
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95307
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2025-01-22 00:00:00
Bug Depends on:    
Bug Blocks: 55004    

Description Darrell Wright 2019-11-07 18:00:25 UTC
It looks like there is a regression in trunk(20171107 on CE) that allows reinterpret_casts in constant expressions.
https://gcc.godbolt.org/z/m7Qytn


using size_t = decltype(sizeof(int));

constexpr char const * func( char const * ptr ) {
    return reinterpret_cast<char const *>( ptr );
}

static_assert( func( nullptr ) == nullptr );

template<size_t N>
constexpr char const * func2( char const (&s)[N] ) {
    return reinterpret_cast<char const *>( s );
}

static_assert( *func2( "Hello" ) == 'H' );

I think this should be ill formed with a diag according to the following links, but it is allowed 
https://eel.is/c++draft/expr.const#4.15
https://eel.is/c++draft/dcl.pre#6
Comment 1 Darrell Wright 2019-11-27 21:17:17 UTC
sorry, posted incorrect CE link, but code below demonstrates it
Comment 2 Jonathan Wakely 2019-11-28 09:02:51 UTC
I see no regression, it has always been accepted by G++.

Possibly a dup of Bug 82304.
Comment 3 Will Wray 2020-07-22 13:29:57 UTC
This particular cast, from const char array to const char pointer, is implicit
so static_cast suffices (and the reinterpret_cast is implictly a static_cast).

Editing your code sample to change reinterpret_cast to static_cast
it is accepted without diagnostic on gcc, clang, msvc, icc
https://gcc.godbolt.org/z/6a694c

The final comment on the bug linked as a possible duplicate shows that
some fixes were committed last month, fixing other cases.
I guess this particular case can be closed.
Comment 4 Will Wray 2020-07-22 15:30:27 UTC
I mis-read this so was too hasty in suggesting "can be closed".
The standard states that a expression evaluation fails to be a constant
expression if it evaluates "reinterpret_cast" (i.e., by named token,
not by the actual strength of cast required, so seems to be the intent).
It's a 50/50 split with gcc & icc accepting, clang and msvc rejecting
https://gcc.godbolt.org/z/drdT66