Domanda

I have a wrapper class to wrap an std::vector<int> data member. I want the wrapper's constructor to forward its arguments to the vector<int>'s ctor. I tried to use a variadic template constructor to implement this. Since std::vector<int> v{1, 2, 3} is constructed by its std::initializer_list ctor, I want the wrapper to activate vector’s initializer_list ctor when I do

wrapper mywrap{1, 2, 3}.

But I didn't get the desired result:

#include <iostream>
#include <vector>

struct wrapper {
    std::vector<int> v;
    template <typename ...Tn> wrapper(Tn ...args) : v(args...) {}
};

int main() {
                                                            //----Output----
    wrapper wrap1{ 1 };
    std::cout << wrap1.v.size() << std::endl;               // 1
    std::cout << wrap1.v.at(0) << std::endl;                // 0
    wrapper wrap2{ 1, 2 };
    std::cout << wrap2.v.size() << std::endl;               // 1
    std::cout << wrap2.v.at(0) << std::endl;                // 2
    wrapper wrap3{ 1, 2, 3 };                               // error
    wrapper wrap4{ std::initializer_list<int>{ 1, 2, 3 } }; // OK
}

The output shows:

wrapper wrap1{ 1 }; calls explicit vector (size_type n);.

wrapper wrap2{ 1, 2 }; calls

vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());.

wrapper wrap3 fails when initializer_list<int> is not specified.

In all cases, I wanted the vector’s initializer_list<int> ctor to be activated. How can this be fixed?

È stato utile?

Soluzione

It's difference between initialization by () and initialization by {}, in your constructor you should also use {} initialization.

template <typename ...Tn> wrapper(Tn ...args) : v{args...} {}

By the way, if you want only initializer_list c-tor, why not simply write this c-tor?

wrapper(std::initializer_list<int> args) : v(args) {}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top