Initializer list woes

Okay. So what I'm trying to do here is pass a number of string objects as function parameters but (love my skills) I messed up with initializer_list. Big time.

1
2
3
4
5
6
7
  void print_string (vector <string> vec)
{
...
}

print_string ({"...", "Some more", ... "As many as wanted"});


But apparently I cannot convert an initializer list to a vector. Even if I use auto, I cannot get the program to work. Any workarounds? Thanks.
> What is the text of the error diagnostic?

This works as expected:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

void print_strings( std::vector<std::string> vec )
{
    for( const auto& str : vec ) std::cout << str << " -> " ;
    std::cout << ".\n" ;
}

int main()
{
    print_strings( { "pass", "an", "initializer", "list", "where", "a", "vector", "is", "expected" } ) ;
}

http://coliru.stacked-crooked.com/a/ee1d380e399b6ece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

void print_string (const std::vector <std::string>& vec)
{
    for (const auto& elem : vec)std::cout << elem << "\n";
}


int main()
{
   print_string (std::vector<std::string>{"...", "Some more", "As many as wanted"});
}
The error says that:

Cannot convert <brace enclosed initializer list> to std::vector
It would appear that you need to upgrade the compiler/library to a more current version.
Topic archived. No new replies allowed.