{ edited by mod to shorten lines to ~70 characters. -mod }translation
Hi!
I was thinking about what some people including I are trying with
transducers in C++14, which involves defining global lambdas [1] [2].
However, I am becoming afraid that this in fact violates the One
Definition Rule. Not only is the object defined in multiple
units, it is even defined with different types!
Hi!
Thanks a lot for your response, it's inspiring...
The difficult problem with lambdas is that they are not constexpr.
Agreed...
In the Fit library[1], I would like to support the factory pattern for lambdas, so you could write this:
auto lambda_factor()
{
return []{ ... };
}
FIT_STATIC_FUNCTION(lambda) = fit::indirect(&lambda_factor, fit::apply);
The fit::indirect adaptor needs to be extended to support a custom operator.
That sounds interesting, but if I am understanding this correctly after looking at the Fit code, this will call `lambda_factor` via a function pointer whenever we want to call `lambda`. While there is some chance
that it will still be inlined, I don't totally like it.
I guess an
alternative, but again more boilerplaty, would be to write something like:
struct lambda_factor
{
auto operator()
{
return [] { ... };
}
};
FIT_STATIC_FUNCTION(lambda) = fit::indirect2<lambda_factor>(...);
All this could maybe be wrapped in a macro such one could write
something like:
FIT_GLOBAL_LAMBDA(lambda, ([] {
...
}));
but admittedly it does not feel very nice...
I hope the standard eventually tackles the problem, ideally by making
lambdas constexpr if needed, but I do understand that the problem is
hard for the C++ compilation model...
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 415 |
Nodes: | 16 (2 / 14) |
Uptime: | 57:54:09 |
Calls: | 8,729 |
Calls today: | 2 |
Files: | 13,277 |
Messages: | 5,958,706 |