Bug 89287 - [n3323] Array declaration fails to use template conversion operator
Summary: [n3323] Array declaration fails to use template conversion operator
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2019-02-11 16:30 UTC by Raphael Kubo da Costa
Modified: 2021-08-27 22:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Raphael Kubo da Costa 2019-02-11 16:30:48 UTC
(This is a reduction from a GCC + Chromium build issue)

Building the following excerpt with -std=gnu++14 causes GCC to fail, while MSVC, clang and ICC work fine:

struct S {
  template <class U>
  constexpr operator U() const {
    return static_cast<U>(42);
  }
};

void frob() {
  new char[S()];
}

x.cc: In function ‘void frob()’:
x.cc:9:14: error: default type conversion can't deduce template argument for ‘template<class U> constexpr S::operator U() const’                                                                  
   new char[S()];
              ^

C++14 says in 8.3.4 Arrays that "If the constant-expression (5.20) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero", so I was expecting `new char[S()]' to end up invoking S::operator size_t().
Comment 1 Marek Polacek 2019-02-11 17:36:39 UTC
This doesn't seem to ever work with G++ (even 4.8 rejects it).
Comment 2 Andrew Pinski 2021-08-27 21:53:46 UTC
Confirmed.  Looks like C++14 changed it to do a conversion to size_t.

Obvious workaround:
new char[(size_t)S()];

The other thing I noticed is ICC accepts it for C++11 while rejects it for C++98 which is unlike clang which rejects it for both C++98 and C++11.

There must have been a C++ defect report ...
Comment 3 Andrew Pinski 2021-08-27 21:59:49 UTC
Looks like n3323 was never implemented.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3323.pdf

Also note there is DR 1464 too.
Comment 4 Andrew Pinski 2021-08-27 22:07:30 UTC
Looks like only part of n3323 was implemented.