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