C++ Logo

std-discussion

Advanced search

Re: Immediate functions

From: Russell Shaw <rjshaw_at_[hidden]>
Date: Mon, 27 Jan 2025 00:41:59 +1100
On 27/1/25 00:31, Jens Maurer wrote:
>
>
> On 26/01/2025 14.21, Russell Shaw via Std-Discussion wrote:
>> Hi,
>>
>> In [expr.const] p.154:
>>
>> ---------------------------------------------------------------
>> An immediate function is a function or constructor that is
>> — declared with the consteval specifier, or
>> — an immediate-escalating function F whose function body contains an
>> immediate-escalating expression E such that E ’s innermost enclosing non-block
>> scope is F ’s function parameter scope.
>>
>> example 9
>>
>> consteval int id(int i) { return i; }
>>
>> constexpr char id(char c) { return c; }
>>
>> template<class T>
>> constexpr int f(T t) {
>> return t + id(t);
>> }
>>
>> auto a = &f<char>; // OK, f<char> is not an immediate function
>>
>> auto b = &f<int>; // error: f<int> is an immediate function
>> ---------------------------------------------------------------
>>
>> The immediate-escalating function 'f' function parameter scope does not contain
>> an immediate-escalating expression.
>
> "f" is not a function, but a function template.
>
>> The 'f' instantiation declarations are: 'int
>> f(int t)' and 'int f(char t)'.
>
> Yes, so what?
>
>> What is wrong ?
>
> Nothing is wrong (given the information you provided).
> What is your question?

The example says:
   auto b = &f<int>; // error: f<int> is an immediate function

But the instantiated function is:

   constexpr int f(int t) {
     return t + id(t);
   }

The function parameter scope is '(int t)' (or does it encompass the body too ?)

Where is the immediate-escalating expression E such that E ’s innermost
enclosing non-block scope is F ’s function parameter scope ?

Received on 2025-01-26 13:42:07