Skip to content

Commit

Permalink
Add aggregator macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Sep 3, 2020
1 parent adc8b0a commit ee20f1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/alia/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ struct is_invocable
#define alia_lambdify(f) ALIA_LAMBDIFY(f)
#endif

// ALIA_AGGREGATOR(f) produces a lambda that assembles its arguments into an
// aggregate expression (i.e., "{args...}") and passes that into f.
// This is useful, for example, when you want to explicitly refer to the
// aggregate constructor of a type as an invocable function.
#define ALIA_AGGREGATOR(f) [](auto&&... args) { return f{args...}; }
#ifndef ALIA_STRICT_MACROS
#define alia_aggregator(f) ALIA_AGGREGATOR(f)
#endif

// function_view is the non-owning equivalent of std::function.
template<class Signature>
class function_view;
Expand Down
15 changes: 15 additions & 0 deletions unit_tests/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ TEST_CASE("exception", "[common]")
REQUIRE(e.what() == std::string("just a test\nin here"));
}

struct aggregatable_struct
{
int a;
std::string b;
};

TEST_CASE("ALIA_AGGREGATOR", "[common]")
{
std::function<aggregatable_struct(int, std::string)> f;
f = ALIA_AGGREGATOR(aggregatable_struct);
auto x = f(4, "foo");
REQUIRE(x.a == 4);
REQUIRE(x.b == "foo");
}

int
invoke_function_views(function_view<int(bool)> a, function_view<int(int)> b)
{
Expand Down

0 comments on commit ee20f1e

Please sign in to comment.