I m a little curious about some of the new features of C++0x. In particular range-based for loops and initializer lists. Both features require a user-defined class in order to function correctly.
I came accross this post, and while the top-answer was helpful. I don t know if it s entirely correct (I m probably just completely misunderstanding, see 3rd comment on first answer). According to the current specifications for initializer lists, the header defines one type:
template<class E> class initializer_list {
public:
initializer_list();
size_t size() const; // number of elements
const E* begin() const; // first element
const E* end() const; // one past the last element
};
You can see this in the specifications, just Ctrl + F class initializer_list .
In order for = {1,2,3}
to be implicitly casted into the initializer_list
class, the compiler HAS to have some knowledge of the relationship between {}
and initializer_list
. There is no constructor that receives anything, so the initializer_list as far as I can tell is a wrapper that gets bound to whatever the compiler is actually generating.
It s the same with the for( : )
loop, which also requires a user-defined type to work (though according to the specs, updated to not require any code for arrays and initializer lists. But initializer lists require <initializer_list>
, so it s a user-defined code requirement by proxy).
Am I misunderstanding completely how this works here? I m not wrong in thinking that these new features do infact rely extremely heavily on user code. It feels as if the features are half-baked, and instead of building the entire feature into the compiler, it s being half-done by the compiler and half done in includes. What s the reason for this?
Edit: I typed rely heavily on compiler code , and not rely heavily on user code . Which I think completely threw off my question. My confusion isn t about new features being built into the compiler, it s things that are built into the compiler that rely on user code.