J
J
Julia2016-09-14 10:38:13
C++ / C#
Julia, 2016-09-14 10:38:13

How to create an object with std::initializer_list?

Hello everyone
I have a class with the following constructors

public:
  std::vector<T>  v; 
  SearchVektor(){};
  SearchVektor(const initializer_list<T> & t ) {
    for( auto a : t) {
      v.push_back(a);
      }
    };

There is a method
SearchVektor<unsigned int>  search ( T num) {
     vector<int> liste;   // --------  search result 
     int begin = 0;
     int mitt = v.size()/2;
     int end =  v.size();		   		  
     thread t1(Work(begin, mitt, num,   std::ref(liste)));			// тут будет 	  liste заполнено 
     thread t2(Work(mitt+1, end, num,    std::ref(liste)));
     t1.join();
     t2.join();
       
  
    SearchVektor<T> t(liste) ;	 
    return 	 t; ;  ----> Моя цель
       
        }

I want to create a new object using vector and not write a new constructor in doing so.
How to proceed ???
Thanks everyone!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2016-09-14
@Mercury13

This is not possible, initializer_list is a thin layer that allows you to initialize a vector from a linear array. She doesn't need anything else.
You have some oddity - SearchVektor<unsigned int>we initialize from a vector vector<int>. It is generally not written in any gate. For this case, you need to...
1. vector<int> listeturn it into vector<unsigned int>.
2. Write a constructor that works from a temporary vector: SearchVektor(std::vector<T> && t ). Such a constructor will be very fast, because it takes all the information from t, leaving a void in it.
3. Declaring liste temporary -

return  SearchVektor< unsigned  int> (std::move(liste));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question