3

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' } };
max66
  • 65,235
  • 10
  • 71
  • 111
Arwed Mett
  • 2,614
  • 1
  • 22
  • 35
  • 1
    Would be happy to see the solution for this... – LernerCpp Aug 02 '20 at 09:20
  • The first is most likely not possible since you need the size of the `std::initializer_list` which you can't get. The second is definately not possible since initializer lists can't have different types. Templates and un-typed brace-lists is seldom a good idea and never easy to work with. – super Aug 02 '20 at 10:20
  • As a side note, what's wrong with `Y my_var{ Y{1,2,3}, Y{1, 'a'} };`? – super Aug 02 '20 at 10:23
  • Ok, too bad. I guess this works too. It would be just nice to omit the type there. – Arwed Mett Aug 02 '20 at 10:24

2 Answers2

2

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> );
 }
max66
  • 65,235
  • 10
  • 71
  • 111
1

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

  • This works but fails when the initializer list is not homogenous. Also I am unsure that it has the type `y, y, y>`. It rather holds the initializer lists. – Arwed Mett Aug 02 '20 at 10:17