Problem:
I want to have a deduction guide for a class that takes a variable number of objects that are constructed by a variadic template.
E.g.
template<typename... Ts>
struct y {
using values_t = std::tuple<Ts...>;
values_t values;
constexpr y(const Ts... values): values( { values... } ) { }
};
I now want to provide a deduction guide such that if I call:
y y { { 1, 2, 3 }, { 'a', 'b', 'c' } };
I will get an object of type:
y<y<int, int, int>, y<char, char, char>> y { { 1, 2, 3 }, { 'a', 'b', 'c' } };
Therefore every time I only provide an initializer list as an argument,
it should be deduced to a "y" object.
Example with simple class:
If I only have a class that has one template parameter I can achieve this by:
template<typename T>
struct x {
T value;
constexpr x(const T value): value(value) { }
};
template<typename T>
x(const std::initializer_list<T>) -> x<x<T>>;
//works -> x x { { 1 } };
EDIT:
Also the following should work:
y y { { 1, 'a' } };
//resolves to -> y<y<int, char>> y { { 1, 'a' } };
Not sure to understand what do you exactly want but...
What about passing through C-style arrays instead of initializer lists?
It required a little of helpers (maybe can be simplified a little)
template <typename T, std::size_t>
using getType = T;
template <typename T, std::size_t ... Is>
auto getY (std::index_sequence<Is...>)
-> y<getType<T, Is>...>;
template <typename T, std::size_t N>
using proY = decltype(getY<T>(std::make_index_sequence<N>{}));
but this deduction guide should works for the nested-homogeneus case
template <std::size_t ... Dims, typename ... Ts>
y ( Ts const (&...arr)[Dims] ) -> y<proY<Ts, Dims>...>;
The following is a full compiling example
#include <tuple>
#include <iostream>
template <typename... Ts>
struct y
{
using values_t = std::tuple<Ts...>;
values_t values;
constexpr y (Ts const & ... vs) : values({ vs... })
{ }
};
template <typename T, std::size_t>
using getType = T;
template <typename T, std::size_t ... Is>
auto getY (std::index_sequence<Is...>)
-> y<getType<T, Is>...>;
template <typename T, std::size_t N>
using proY = decltype(getY<T>(std::make_index_sequence<N>{}));
template <std::size_t ... Dims, typename ... Ts>
y ( Ts const (&...arr)[Dims] ) -> y<proY<Ts, Dims>...>;
int main()
{
using T0 = decltype(y{ { 1, 2 }, { 'a', 'b', 'c' },
{1.0, 2.0, 3.0, 4.0} });
using T1 = y<y<int, int>, y<char, char, char>,
y<double, double, double, double>>;
static_assert( std::is_same_v<T0, T1> );
}
This ?
#include <tuple>
#include <iostream>
using namespace std;
template<typename... Ts>
struct Y {
using values_t = std::tuple<Ts...>;
values_t values;
constexpr Y(const Ts... values): values( { values... } ) { }
};
template<typename ... T>
Y( const std::initializer_list<T>&... ) -> Y< std::initializer_list<T>... >;
int main() {
Y y { {1, 2, 3}, {'a', 'b'}, {1.5, 100.223, 10.1, 5.6, 6.6} };
Y y2{ 19, 1.4, 'a'};
std::cout << "y:" << sizeof(y) << " y2:" << sizeof(y2);
}
https://godbolt.org/z/vrGdWb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With