Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

templated lambda issue

14 views
Skip to first unread message

Bonita Montero

unread,
Jun 14, 2021, 7:10:23 AM6/14/21
to
int main()
{
auto f = []<typename T>() -> T
{
return 123;
};
f.operator ()<int>(); // only way ?
}

Is the above code the only way to call the lambda ?

David Brown

unread,
Jun 14, 2021, 8:24:11 AM6/14/21
to
You made a templated lambda - you have to give the type /somehow/. What
are you actually trying to do?

Bonita Montero

unread,
Jun 14, 2021, 8:58:33 AM6/14/21
to
>> int main()
>> {
>>     auto f = []<typename T>() -> T
>>     {
>>         return 123;
>>     };
>>     f.operator ()<int>(); // only way ?
>> }
>>
>> Is the above code the only way to call the lambda ?

> You made a templated lambda - you have to give the type /somehow/.

No, I just want to return a value of that type.

Alf P. Steinbach

unread,
Jun 14, 2021, 9:12:56 AM6/14/21
to
I've never used a templated lambda, but if I needed something like that
I think I'd use a type-indicating argument, like (off the cuff)

template< class T > struct Use_type_{ using Type = T; };

auto main() -> int
{
const auto f = []( auto t )
{ return static_cast<decltype(t)::Type>( 123 ); };

return f( Use_type_<int>() );
}

- Alf

MrSpook...@qvqo7xsmj4s.co.uk

unread,
Jun 14, 2021, 9:16:41 AM6/14/21
to
On Mon, 14 Jun 2021 15:12:37 +0200
"Alf P. Steinbach" <alf.p.s...@gmail.com> wrote:
>On 14 Jun 2021 13:10, Bonita Montero wrote:
>> int main()
>> {
>>     auto f = []<typename T>() -> T
>>     {
>>         return 123;
>>     };
>>     f.operator ()<int>(); // only way ?
>> }
>>
>> Is the above code the only way to call the lambda ?
>
>I've never used a templated lambda, but if I needed something like that
>I think I'd use a type-indicating argument, like (off the cuff)
>
> template< class T > struct Use_type_{ using Type = T; };
>
> auto main() -> int

Or back in the real world: int main()


0 new messages