User Tools

Site Tools


cpp:aggregate_binding
snippet.cpp
#include <string_view>
using namespace std::literals;
 
template<class... Ts> 
struct aggregate_binding : Ts... 
{ 
    aggregate_binding(Ts... t) : Ts(t)... {} 
    using Ts::operator()...; 
    template<typename K>
    auto operator()(K const& k) const
    {
        return k;
    }
};
 
template<class... Ts> 
aggregate_binding(Ts...) -> aggregate_binding<Ts...>;
 
template<typename T> struct box {};
 
template<typename Key, typename Value>
auto bind(Value const& v)
{
  return [v](box<Key> const&) { return v; };
}
 
template<int N>
struct parameter
{ 
  template<typename T>
  auto operator=(T const& v) const { return bind<parameter>(v); }   
};
 
parameter<0> const x_ = {};
parameter<1> const y_ = {};
parameter<2> const z_ = {};
 
template<typename... Things>
auto make_typed_tuple(Things const&... ts)
{
    return aggregate_binding(ts...);
}
 
template<typename K> auto f(K const&)
{
    auto u = make_typed_tuple( x_ = 1.5, y_ = "test"sv);
    return u(box<K>{});
}
 
auto x = f(x_);
auto y = f(y_);
cpp/aggregate_binding.txt · Last modified: by allspark