Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward std::initializer_list<T>?

I am trying to forward a std::initializer_list but

no known conversion from 'std::initializer_list<A>' to 'std::initializer_list<B>'

Here is the test code

#include <iostream>

class B {
};

class A: public B {
};

class not_working {
    private:
    void fun(std::initializer_list<B> p) {
    }
    public:
    template<typename T>
    not_working(std::initializer_list<T> args) {
        fun(args);
    }
};

class working {
    private:
    void fun(std::initializer_list<B> p) {
    }
    public:
    working(std::initializer_list<B> args) {
        fun(args);
    }
};

int main(){    
    working{A{}, A{}};
    //not_working{A{}, A{}};
}

How can I forward the std::initializer_list
without explicit casting not_working{(B)A{}, (B)A{}}; ?

Why is this a problem for me ?

I have a proxy-class that forwards the constructor-parameters to a class. Something like this:

 template<typename T>
 class proxy {
    T real;
    template<typename S> proxy(std::initializer_list<S> p): real(p) {}
    template<typename S...> proxy(S ... p): real(p ...) {}
};
like image 834
KoKuToru Avatar asked Aug 31 '14 13:08

KoKuToru


Video Answer


2 Answers

You cant, and the reason is the same as why you also could not cast a std::vector<A> to a std::vector<B>. Containers of different types are completely unrelated.

The only way to "change" the type of a container is to construct a new container of the new type (eg, std::vector<B>(a.begin(), a.end()) -- but beware of slicing!).

like image 177
Jared Grubb Avatar answered Sep 22 '22 10:09

Jared Grubb


Posting answer from comments of question to increase visibility of the solution:

Standard solution is to use std::forward. This fails forwarding initializer lists.

template<typename T>
     class proxy {
        T real;
        public:
            template<typename ... S> proxy(S ... p): real{std::forward<S>(args)...} {}
    };

Using std::move, also works with std::initializer_list (provided by @dyp) :

template<typename T>
 class proxy {
    T real;
    public:
        template<typename ... S> proxy(S ... p): real{std::move(p) ...} {}
};
like image 34
JE42 Avatar answered Sep 22 '22 10:09

JE42