Re: constexpr memeber function not constexpt anymore in v10

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 19 Feb 2020 at 10:10, Klaus Doldinger
<klaus.doldinger64@xxxxxxxxx> wrote:
>
>
>
> Am 19.02.20 um 11:02 schrieb Jonathan Wakely:
> > On Wed, 19 Feb 2020 at 07:53, Klaus Doldinger
> > <klaus.doldinger64@xxxxxxxxx> wrote:
> >>
> >> The following was possible in v9:
> >>
> >> template<auto N>
> >> struct A {
> >>     constexpr auto size() const {
> >>         return N;
> >>     }
> >> };
> >>
> >> template<typename A>
> >> void foo(const A& a) {
> >>     constexpr auto s = a.size();
> >> }
> >>
> >> int main() {
> >>     A<10> x1;
> >>     foo(x1);
> >> }
> >>
> >> In v10 this is rejected.
> >>
> >> Looks like a bug?
> >
> > No, I don't think so. The parameter a is not a constant expression, so
> > a.size() is not a constant expression, so can't be used to initialize
> > a constexpr variable.
> >
>
> That would be weird.
> If so, the follwing would be impossible:
>
>     std::array<int, 10> a;
>     std::array<int, a.size()> b;


That's not at all equivalent.

A function with the constexpr specifier doesn't mean it can always be
used in a constant expression, only that it can be used in a constant
expression when called with constant arguments (including the implicit
'this' argument for a member function).

In your case you are trying to use a.size() inside a non-constexpr
function, with a non-constant argument. That means a.size() is not a
constant. So can't initialize a constexpr variable.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux