Hello!
I'd like to ask you something I've been looking for without luck: moving from
std::initializer_lists. It's not possible with the current revision of the standard, but, is it planned in the future? I think it would be something very useful.
Let's take some code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <initializer_list>
#include <vector>
#include <string>
#include <iostream>
class Example
{
private:
std::vector<std::string>
L;
public:
explicit
Example
( const std::initializer_list<std::string> & l )
: L(l)
{
return;
};
void
print
( void )
{
for ( const auto & v : L )
std::cout << v << std::endl;
return;
};
};
int
main
( void )
{
Example example{ "C++" , "mhgc" , "Warcraft" };
return 0;
}
|
If I have not taken it wrong, three strings would be created to fill the first and only argument of the constructor of
Example. As the constructor accepts a
const reference, the
std::initializer_list object wouldn't be copied, so nothing would happen to the strings. Fast and clear.
But then, when I initialize the vector, each string must be copied, as
std::initializer_lists do not allow moving from them. I think this is a bit annoying: what if they weren't simple strings? I usually think of temporaries whenever I see "std::initializer_list"; why don't they help using move semantics?
So, are there any plans to fix this, any workarounds? I would prefer something easy and "elegant". That's why I chose
std::initializer_lists: they seemed wonderful.
Thanks! Best regards,
Kalrish