Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Initializer list

I need to convert a class written in C++ 0x to one which compiles in Visual studio 2008. The code uses std::initializer_list.

Following is the code

template <typename data_type>
class symmatrix
{
public:


    typedef data_type         value_type;
    symmatrix(std::initializer_list<T> const& size, value_type ini = value_type())
      : m_data(0), m_memory(false) { resize(size); *this = ini; }
}

has to be converted to old standard understood by VS 2008.

I am really struggling to change 100 lines of new C++ code to old C++. So, please help me.

like image 623
Arjun Avatar asked Jul 25 '11 07:07

Arjun


People also ask

What is an initializer list used for?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

Are initializer lists faster?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment. Note: There is no performance difference if the type of x_ is some built-in/intrinsic type, such as int or char* or float.

What is std :: initializer list?

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .

Does initializer list run before constructor?

The initializer list is used to directly initialize data members of a class. An initializer list starts after the constructor name and its parameters.


1 Answers

Instead of an initializer_list you can choose to pass a pair of iterators. But you'll have to change client code as well.

If it is a well-written class, it's bound to have other constructors, such as the one I mentioned. In this case I'd recommend to just remove the overload that takes an initializer_list. Client code may have to be changed as well, if it uses that constructor.

like image 94
Armen Tsirunyan Avatar answered Oct 13 '22 19:10

Armen Tsirunyan