Date: Tue, 12 Aug 2025 09:35:20 +0200
Am Dienstag, dem 12.08.2025 um 09:14 +0200 schrieb Jens Maurer:
> Let's get this bit out of the way separately:
>
> On 12.08.25 08:46, Martin Uecker wrote:
> > My apologies for distracting you with C array, but the fundamental
> > name lookup issue affects also C++ library types:
> >
> > template<typename B>
> > void foo(std::array<float, sizeof(B)>, B x);
>
> There is no name lookup problem here:
>
> template<typename B>
>
> declares the name B to refer to an (unknown) type.
>
> void foo(std::array<float, sizeof(B)>, B x);
>
> The "sizeof(B)" refers to the "B" introduced by the template-head,
> not to anything mentioned in the parameter that follows. For example,
>
> template<typename B>
> void foo(std::array<float, sizeof(B)>);
>
> is also a well-formed declaration.
Yes. Sorry, I wasn't clear. My point was that if you wanted
to avoid the "template" notation and write this with "auto"
like this
void foo(std::array<float, sizeof(b)>, auto b);
this would not work. But my impression was that C++ is
moving towards this nicer way of writing templates.
Martin
>
>
> And "B x" declares the second parameter "x" to be of that yet-unknown
> type "B".
>
> The entirety makes up a declaration of a function template,
> which is not (yet) a function. In order to turn a function
> template into a function, instantiation needs to happen,
> which replaces all occurrences of yet-unknown types with
> known ones. This way, several different functions (one for
> each combination of specific types for the set of unknown
> types) can be produced from a single function template.
> The compiler generates no assembly code for a function template.
>
> Jens
> Let's get this bit out of the way separately:
>
> On 12.08.25 08:46, Martin Uecker wrote:
> > My apologies for distracting you with C array, but the fundamental
> > name lookup issue affects also C++ library types:
> >
> > template<typename B>
> > void foo(std::array<float, sizeof(B)>, B x);
>
> There is no name lookup problem here:
>
> template<typename B>
>
> declares the name B to refer to an (unknown) type.
>
> void foo(std::array<float, sizeof(B)>, B x);
>
> The "sizeof(B)" refers to the "B" introduced by the template-head,
> not to anything mentioned in the parameter that follows. For example,
>
> template<typename B>
> void foo(std::array<float, sizeof(B)>);
>
> is also a well-formed declaration.
Yes. Sorry, I wasn't clear. My point was that if you wanted
to avoid the "template" notation and write this with "auto"
like this
void foo(std::array<float, sizeof(b)>, auto b);
this would not work. But my impression was that C++ is
moving towards this nicer way of writing templates.
Martin
>
>
> And "B x" declares the second parameter "x" to be of that yet-unknown
> type "B".
>
> The entirety makes up a declaration of a function template,
> which is not (yet) a function. In order to turn a function
> template into a function, instantiation needs to happen,
> which replaces all occurrences of yet-unknown types with
> known ones. This way, several different functions (one for
> each combination of specific types for the set of unknown
> types) can be produced from a single function template.
> The compiler generates no assembly code for a function template.
>
> Jens
Received on 2025-08-12 07:35:24