C++ Initializer_List into Vector of Pointers -


i've been trying streamline of code, condensing , making calls easier can. 1 of things i've been trying fuse 2 common overloads have on place one. follows:

void myclass::addsomething(something & a) {    vectorofsomething.push_back(&a) }  void myclass::addsomething(std::vector<something*> a) {    (unsigned int = 0; < a.size(); i++)       vectorofsomething.push_back(a[i]); } 

the class abstract. vectorofsomething vector of pointers somethings.

essentially, instead of having this:

std::vector mysomethings = {&something1, &something2}; addsomething(mysomethings); 

i want this:

addsomething({something1, something2}); 

so more similar first overload (no need user create vector of pointers first). looking around 2 ways saw either through: std::initializer_list or variadic templates, both of not afraid admit foreign me.

i've given shot initializer lists have run ton of errors trying various things. i've managed function accept list, can't quite figure out how populate vector correctly. main issue seems init list has values 'const' within.

if has idea on how make work grateful!

something :

#include <iostream> #include <initializer_list> #include <vector> #include <list>  struct myclass {     template <typename >     void add( b, e ) {         data_.insert( end(data_), b, e );     }      void add( std::initializer_list<int> const & elems ) {          add( begin(elems), end(elems) );      }      template <typename t>     void add( t const & cont ) {         add( begin(cont), end(cont) );      }      std::vector<int> data_;      friend std::ostream & operator<<( std::ostream & os, myclass const & mc ) {         for( auto & e : mc.data_ )             os << e << " ";         return os;     } };  int main() {     myclass foo;      foo.add( { 1, 3 });      std::vector<int> v { 1,2};     foo.add( v );      std::list<int> l { 4,5};     foo.add( l );      std::cout << foo; } 

a compiler cannot infer template argument initializer_list, specialization has explicit.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -