Returning variadic aggregates (struct) and syntax for C++17 variadic template 'construction deduction guide'
In C++17 aggregate initialization will be able to initialize public base classes. So you can use inheritance + pack expansion to build such class. To make it work with structured bindings, you will have to expose tuple interface: specialize std::tuple_size
and std::tuple_element
and provide get
function for your class:
//Headers used by "many" class implementation
#include <utility>
#include <tuple>
namespace rw {
namespace detail {
template <size_t index, typename T>
struct many_holder
{ T value; };
template <typename idx_seq, typename... Types>
struct many_impl;
template <size_t... Indices, typename... Types>
struct many_impl<std::index_sequence<Indices...>, Types...>: many_holder<Indices, Types>...
{};
}
template <typename... Types>
struct many: detail::many_impl<typename std::make_index_sequence<sizeof...(Types)>, Types...>
{};
template<size_t N, typename... Types>
auto get(const rw::many<Types...>& data) -> const std::tuple_element_t<N, rw::many<Types...>>&
{
const rw::detail::many_holder<N, std::tuple_element_t<N, rw::many<Types...>>>& holder = data;
return holder.value;
}
}
namespace std {
template <typename... Types>
struct tuple_size<rw::many<Types...>> : std::integral_constant<std::size_t, sizeof...(Types)>
{};
template< std::size_t N, class... Types >
struct tuple_element<N, rw::many<Types...> >
{ using type = typename tuple_element<N, std::tuple<Types...>>::type; };
}
//Headers used for testing
#include <iostream>
#include <string>
int main()
{
rw::many<int, std::string, int> x = {42, "Hello", 11};
std::cout << std::tuple_size<decltype(x)>() << '\n' << rw::get<1>(x);
}
Demo (right now works only in clang 3.9): http://melpon.org/wandbox/permlink/9NBqkcbOuURFvypt
Notes:
- In demo there is a commented out implementation of
nth_type
, you can use to inplementtuple_element
directly and not defer it totuple_element<tuple>
implementation. get<many>
should be either a member function ofmany
or be placed in associated namespace for structured bindings to work. You should not overloadstd::get
(It would be UB anyway).- I left implementation of
get
for non-const references and r-value references as excercise for the reader. - There is no example of using structured bindings and guides, because clang does not support them (in fact I do not know any compiler which supports them). In theory
template<typename... Types> many(Types...) -> many<Types...>;
should work.
There was a discussion about this on std-proposals just the other day.
We don't have final wording yet, or for that matter a compiler (that I'm aware of) that supports deduction guides, but according to Richard Smith the following deduction guide should work (precis'd):
template<class A, class B>
struct Agg
{
A a;
B b;
};
template<class A, class B>
Agg(A a, B b) -> Agg<A, B>;
Agg agg{1, 2.0}; // deduced to Agg<int, double>
So yes, a variadic deduction guide for an aggregate should also work and will work with aggregate initialization syntax. It won't work without a deduction guide, as without a deduction guide the compiler needs a constructor.