Last Updated: September 08, 2017
·
369
· themichael'tips

C++11 Variadic Template : a real useful example

A common way to split a string might look like as : std::vector<std::string> v = split("::a,,1::3.14 ", " :,").

Using C++11 Variadic Template I've create a smart way to split string. The relative gist is here.

The idea is to pass a list of variables, of any type, as argument of the split function in order to store in each specific variable of the list the relative value selected from the string.

An example could be :

std::string first;
int second;
float third;

split(" STRING:: 123 --:: 3.14;;", " -:;", first, second, third);
/* Optional : auto v = split(" STRING:: 123 --:: 3.14;;", " -:;", first, second, third); */

assert(first == "STRING");
//assert(v[0] == first);

assert(second == 123);
//assert(v[1] == "123");

assert(third == 3.14f);
//assert(v[2] == "3.14");

Enjoy!