C++ Logo

std-proposals

Advanced search

Re: Remove non-type template limitations for consteval functions

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 24 Feb 2021 10:08:16 -0500
On Wed, Feb 24, 2021 at 6:58 AM Bastien penavayre via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hi,
> Currently, non-type templates still have several limitations:
> - must be structural (no private members)
> - no allocations even if the type has a constexpr destructor
> - can't contain floating point (double or float)
> - can't hold strings of the "const char *" form
> and so on...

Most of this is wrong. The only thing you got right was "must be
structural," but your definition of "structural" is incorrect.

Floating-point types are structural types, so they can be used as
NTTPs (can we please have a better term for this?).

Structural types can allocate memory. Or more to the point, nothing in
the standard says that they *can't* allocate memory. Pointer types are
scalar types, so you can have those as members.

Speaking of which, structural types can hold `const char*`s. What is
prevented is them holding a pointer that points to a *string literal*.
This is important because whether two string literals that happen to
have the same character values decay to the same pointer is not
defined. And this matters both at compile-time and runtime.

So if you want a string in an NTTP, you need to pass it around by value.

> These limitations have a good reason behind them for templated class and function as they produce symbols in assembly and need to be resolved.
>
> But as consteval functions never produce assembly/symbols and will never be resolved by another translation unit than their own these restrictions should be lifted.

It seems to me what you really want is constexpr parameters. So we
should just *have that* and stop trying to pass NTTPs around as a
substitute.

Received on 2021-02-24 09:08:30