简体   繁体   中英

How to make wrapper class forward its constructor arguments to std::vector's constructor?

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?

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) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM