boostorg / boostorg/hof

Add a macro to reduce boilerplate with the factory pattern

Open
#161 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
509
Forks
94
PR merge metrics
No merged PRs in 30d

Description

An alternative to declaring functions using the `FIT_STATIC_LAMBDA_FUNCTION` is to use the factory pattern in C++14:

``` cpp
struct lambda_factor
{
auto operator*() const
{
return [] { ... };
}
};

FIT_STATIC_FUNCTION(lambda) = fit::indirect(lambda_factor{});
```

It might be possible to have a macro to reduce this boilerplate:

``` cpp
#define FIT_STATIC_FACTORY(name) \
struct name ## _factory { auto operator*() const; } \
FIT_STATIC_FUNCTION(name) = fit::indirect(name ## _factory{}); \
auto name ## _factory::operator*() const
```

Then it could be written as:

``` cpp
FIT_STATIC_FACTORY(lambda)
{
return [] { ... };
}
```

By making it a macro it could fallback on using a lambda in C++11, although this would require a semicolon at the end.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.