std::initializer_list<move_only_type>

This is the worst thing that can happen to you. And it's happened to me twice. Initializer lists in C++11 are such trolls, it's not even funny. I hate having to make elaborate workarounds just because the convenient option doesn't exist.

What do I mean? http://stackoverflow.com/a/8468817/1959975

Classic example? http://stackoverflow.com/a/9618553/1959975

Also on /r/cpp: http://redd.it/1j01yc
Last edited on
I don't know about others, but I'd appreciate it if you would post some example code instead of linking to other people's problems and have us guess what you're refering to.
@Catfish4 I don't know if you don't understand, but it links to the specific reply on the page. So there is no guessing involved when he's showing you exactly where to look(focuses on the comment).
You can't do anything with it at all, it's completely useless. You can make a container with move-only types but you can't initialize it with an initializer list.
Well... they're probably planning to fix this in C++14 then?
> they're probably planning to fix this in C++14 then?

It doesn't need any fix. Programmers desperate for such functionality can always write a small shim.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <initializer_list>

template < typename CNTR, typename T > CNTR make_sequence( const std::initializer_list<T>& il )
{
    CNTR cntr ;
    for( auto& p : il ) cntr.emplace_back(p) ;
    return cntr ;
}

#include <vector>
#include <memory>

int main()
{
    using sequence_type = std::vector< std::unique_ptr<int> > ;
    auto seq = make_sequence<sequence_type>( { new int(0), new int(1), new int(2) } ) ;
}
That doesn't mean it's not annoying ;)
Topic archived. No new replies allowed.