Frage

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.

War es hilfreich?

Lösung

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).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top