Skip to main content
GameDev.net gamedev.net
🔒 Locked

printing an initializer_list

Started by DevFred Jul 4, 2009 at 7:46 AM 0 replies 600+ views
Original Post
DevFred
DevFred
Is there any way I can print an initializer_list via << without being explicit about the fact that it is an initializer_list?
template<typename T>
void print(std::initializer_list<T> const& x)
{
    for (auto it = x.begin(); it != x.end(); ++it)
    {
        // ...
    }
}

template<typename T>
std::ostream& operator<<(std::ostream& os, std::initializer_list<T> const& x)
{
    for (auto it = x.begin(); it != x.end(); ++it)
    {
        // ...
    }
    return os;
}

int main()
{
    print( {4, 8, 15, 16, 23, 42} );     // works
    
    std::cout << {4, 8, 15, 16, 23, 42}; // does not work
    std::cout << std::initializer_list<int>( {4, 8, 15, 16, 23, 42} ); // too clumsy
}
DevFred
DevFred
Hm, I just thought of a way, but maybe there's a better one?
operator<<(std::cout, {4, 8, 15, 16, 23, 42});

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.