C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Constexpr parameters

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Sun, 31 Aug 2025 23:36:55 +0300
On Sun, 31 Aug 2025 at 23:23, Charles R Hogg via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>> In current C++ there are basically three ways to pass in a compile-time constant:
>>
>> as a template argument (in angle brackets),
>> using an ordinary function argument whose type encodes the compile-time constant (for which one or more corresponding template arguments will be deduced), or
>> using the `std::format` trick.
>
> This was really intriguing. I'm very familiar with the first two, but I don't know what "the `std::format` trick" refers to, and naive Googling didn't show me a good reference. Can you please either explain more, or else link to a reference that explains what you mean?

It wraps a format-string to a consteval-only-constructible type. So it
doesn't wrap a constant into a type, but it wraps a constant into an
always-constant
wrapper value. It boils down to the equivalent of

class X
{
  int val;
public:
  consteval X(int v) : val(v) {}
  constexpr int get() {return val;}
};

int runtime() {return 666;}

void other_runtime(X x) {
  int foo = x.get(); // no problem here
}

int main()
{
  X x{42}; // OK
  //X x2{runtime()}; // can't construct X from a runtime value
  other_runtime(x); // this is the equivalent of the "std::format trick"
}

Received on 2025-08-31 20:37:09