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

Why don't havbe lambdas without a capture a default-constructor

56 views
Skip to first unread message

Scott Newman

unread,
Jun 23, 2020, 5:03:33 AM6/23/20
to
I've got a lambda which does hashing a key to a size_t and this lambda
hasn't any captures. So it could be default-constructible so that I
could pass its type as a temmplate-parameter via decltype(lambda).
But lambdas are only copy-constructible. For the case of lambdas
without a capture I consdider this as a language-weakness.

Sam

unread,
Jun 23, 2020, 7:11:59 AM6/23/20
to
This works in C++20, so install the current version of gcc, and have fun:

template<typename T> struct foo : T {};

void bar()
{
foo<decltype( []{})> baz;
}

Scott Newman

unread,
Jun 23, 2020, 7:36:11 AM6/23/20
to
This ain't a lambda.

guinne...@gmail.com

unread,
Jun 23, 2020, 8:13:36 AM6/23/20
to
Are you claiming that []{} is not a lambda?
Good luck with that.

Alf P. Steinbach

unread,
Jun 23, 2020, 11:08:09 AM6/23/20
to
On 23.06.2020 13:11, Sam wrote:
> Scott Newman writes:
>
>> I've got a lambda which does hashing a key to a size_t and this lambda
>> hasn't any captures. So it could be default-constructible so that I
>> could pass its type as a temmplate-parameter via decltype(lambda).

In C++17 and earlier a lambda can't be used in an unevaluated context.


>> But lambdas are only copy-constructible. For the case of lambdas
>> without a capture I consdider this as a language-weakness.
>
> This works in C++20, so install the current version of gcc, and have fun:
>
> template<typename T> struct foo : T {};
>
> void bar()
> {
>     foo<decltype( []{})> baz;
> }

As I see it the OP is concerned with the formal lack of a default
constructor in C++17 and earlier.

The current standard is C++17.

For a workaround that uses only general portable C++17 or earlier
functionality the OP can possibly just define a suitable default
constructible type with a function call operator, like this:


-------------------------------------------------------------------
#include <utility>

template< class Functor >
void foo()
{
for( int i = 1; i <= 3; ++i ) {
Functor f;
f();
}
}

#include <iostream>
using std::cout, std::endl;

auto main()
-> int
{
struct Bah{ void operator()(){ cout << "Bah!" << endl; } };
foo<Bah>();
}
-------------------------------------------------------------------


- Alf

Links: https://en.cppreference.com/w/cpp/language/lambda

Scott Newman

unread,
Jun 23, 2020, 11:58:35 AM6/23/20
to
> auto main()
>     -> int

idiot-style. It's better just because it's newer.

Frederick Gotham

unread,
Jun 24, 2020, 4:15:26 AM6/24/20
to
On Tuesday, June 23, 2020 at 4:08:09 PM UTC+1, Alf P. Steinbach wrote:

> auto main()
> -> int
> {



I claim that I'm the person who started this trend. (Although personally I always put 'void' instead of empty parentheses).

Juha Nieminen

unread,
Jun 24, 2020, 5:37:40 AM6/24/20
to
Clearly there's too much information in one single line there. Rather obviously it should be:

using
Main_Ret_Value_t
=
int;

auto
main
(
void
)
->
Main_Ret_Value_t
{
// your code here
}

Bonita Montero

unread,
Jun 24, 2020, 6:29:20 AM6/24/20
to
> Clearly there's too much information in one single line there. Rather obviously it should be:
> using
> Main_Ret_Value_t
> =
> int;
> auto
> main
> (
> void
> )
> ->
> Main_Ret_Value_t
> {
> // your code here
> }

I hope your joking, otherwise you'd get the studpidity-award.

Sam

unread,
Jun 24, 2020, 7:04:31 AM6/24/20
to
It is, if you actually know C++.

James Kuyper

unread,
Jun 24, 2020, 7:59:52 PM6/24/20
to
On 6/23/20 8:13 AM, guinne...@gmail.com wrote:
> On Tuesday, 23 June 2020 12:36:11 UTC+1, Scott Newman wrote:
...
>>> This works in C++20, so install the current version of gcc, and have fun:
>>> template<typename T> struct foo : T {};
>>>
>>> void bar()
>>> {
>>>     foo<decltype( []{})> baz;
>>> }
>>
>> This ain't a lambda.
>
> Are you claiming that []{} is not a lambda?
> Good luck with that.

I know that this won't do anything to change Scott's opinion on the
matter, but section 8.1.5 gives the relevant grammar productions:

lambda-expression:
lambda-introducer lambda-declarator opt compound-statement
lambda-introducer:
[ lambda-capture opt ]
lambda-declarator:
( parameter-declaration-clause ) decl-specifier-seq opt
noexcept-specifier opt attribute-specifier-seq opt
trailing-return-type opt


"[]" is a lambda-introducer, with the optional lambda-capture missing,
{} is an empty compound-statement. The combination is a
lambda-expression with the optional lambda-declarator missing.

Stuart Redmann

unread,
Jun 25, 2020, 3:32:27 AM6/25/20
to
That can still be improved:
http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html

Regards,
Stuart

0 new messages