lundi 23 avril 2018

C++ 11 nested list (vector of vectors of strings) initialization fails

This code:

#include <vector>
#include <string>
#include <iostream>

class MyClass
{
public:
  MyClass(const std::vector<std::vector<std::string>> & v)
  {
    std::cout << "Vector of string vectors size: " << v.size() << "\n";

    for (size_t i = 0; i < v.size(); i++)
      std::cout << "Vector #" << i << " has size " << v[i].size() << "\n";
  }
};

int main()
{
  MyClass({ { "a" } }); // <--- ok
  MyClass({ { "a", "b" } }); // <--- PROBLEM
  MyClass({ { std::string("a"), "b" } }); // <--- ok
  MyClass({ { "a", "b", "c" } }); // <--- ok
  MyClass({ { "a" },{ "c" } }); // <--- ok
  MyClass({ { "a", "b" },{ "c", "d" } }); // <--- ok
}

outputs this (Visual Studio 2017):

Vector of string vectors size: 1
Vector #0 has size 1
Vector of string vectors size: 4
Vector #0 has size 97
Vector #1 has size 0
Vector #2 has size 0
Vector #3 has size 0
Vector of string vectors size: 1
Vector #0 has size 2
Vector of string vectors size: 1
Vector #0 has size 3
Vector of string vectors size: 2
Vector #0 has size 1
Vector #1 has size 1
Vector of string vectors size: 2
Vector #0 has size 2
Vector #1 has size 2

So, it works OK in all cases except in the case where we have a vector of one vector, containing two strings. It also works in the above case if we explicitly construct std::string from one of the string literals. If both are just plain string literals, the compiler seems to get "confused" and constructs a vector of 4 items, the first of which contains 97 strings. Note that 97 is the character code of "a".

I guess my question is, should the compiler interpret this problematic construction as I'd expect, or is this bad code to initialize a nested list like this?

Aucun commentaire:

Enregistrer un commentaire