user2798943
user2798943

Reputation: 197

convert initializer_list<T> to initializer_list<vector<T>> at compile time

I have a class constructor that accepts an

initializer_list<T>

this constructor has to run the parent class constructor that accepts an

initializer_list<vector<T>>.

so I have to convert the initializer list to an 2d initializer list.

{1, 2, 3, 4} to { {1}, {2}, {3}, {4} }

EDIT:

I have a class constructor that accepts an

initializer_list<T>

this constructor has to run the parent class constructor that accepts an

initializer_list<array<T, 1>>.

so I have to convert the initializer list to an 2d initializer list.

{1, 2, 3, 4} to { {1}, {2}, {3}, {4} }

Upvotes: 4

Views: 899

Answers (3)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48487

Below you can find a complete example if you are still having troubles with mixing <typename T> from class' declaration with <typename... Args> pack within constructor:

#include <iostream>
#include <array>
#include <utility>
#include <initializer_list>

template <typename T>
class A
{
public:
    A(std::initializer_list<std::array<T, 1>> i);

    virtual ~A() = default;
};

template <typename T>
A<T>::A(std::initializer_list<std::array<T, 1>> i)
{
    std::cout << i.size() << std::endl;
}

template <typename T>
class B : public A<T>
{
public:
    template <typename... Args>
    B(Args&&... args);
};

template <typename T>
template <typename... Args>
B<T>::B(Args&&... args) : A<T>{std::array<T, 1>{std::forward<Args>(args)}...}
{

}

int main()
{
    B<int> b{ 1, 2, 3, 4, 5, 6, 7 };
    return 0;
}

Live demo link.

Upvotes: 2

David G
David G

Reputation: 96845

Why not have your child class take parameter pack and forward it off to the parent constructor?

struct Parent
{
    template<typename T>
    Parent(std::initializer_list<std::array<T, 1>>) { }

    virtual ~Parent() = default;
};

struct Child : Parent
{
    template<class... Args>
    Child(Args&&... args) : Parent{{{args}...}} { }
};

Upvotes: 3

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153955

std::initializer_list<T> is an odd hybrid: to create one, you'll need to statically know its size and content but once you get handed one, you can't statically determine its content! Put differently, I don't think you can create an std::initializer_list<std::vector<T>> from an std::initializer_list<T>.

What you can do is to take a variadic argument list, possibly constraint to have all the same type and the type being integral, and construct an std::initializer_list<std::vector<T>> from that:

template <typename T>
void g(std::initializer_list<T> list) { ... }

template <typename... T>
void f(T... args) {
    g({ std::array<T, 1>(args)... });
}

Upvotes: 2

Related Questions