Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my own initializer_list class in C++11?

I made a class that has the same implementation of std::initializer_list using VS2013 that supports Uniform Initialization.

template <class _Elem>
class My_Initializer_List { ... } // Hope to replace std::initializer_list!

The first thing I've noticed is that the compiler keeps looking for std::initializer_list when it does have the substitute for it, which is My_Initiliazer_List.

void Foo()
{
    // The compiler tries to invoke 
    // its constructor with std::initializer<int>{ 10, 20 }.
    auto MyVar = My_Initializer_List<int>{ 10, 20 }; // Compile Error!
}

If this feature depends on the C++ Standard Library, then I believe that I should be able to replace it with my own class, at least for academic purposes.

Thanks in advance.

like image 243
Dean Seo Avatar asked Nov 14 '13 03:11

Dean Seo


1 Answers

Sorry, but no. The compiler has special knowledge of std::initializer_list "baked in". The same is true of a few other parts of the standard library (e.g., a dynamic_cast of a reference throws a std::bad_cast, and there's not really anything you can do to change that either).

like image 186
Jerry Coffin Avatar answered Sep 27 '22 18:09

Jerry Coffin