jeudi 21 juillet 2016

Best way to store and construct member const array or strings

Suppose I have a class like this:

class foo {
  const A arr;
  foo(B arg): arr(arg) { }
};

What is the most efficient choice for the types of A and B. I was thinking something like this,

class foo {
  const std::initializer_list<std::string> arr;
  foo(std::initializer_list<std::string> arg): arr(arg) { }
};

But I think this will copy-construct strings in arr. I'd like to be able to do this:

std::string str1("there"), str2("now");
foo bar({"hello",str1,std::move(str2)});

and have rvalue strings in the brackets moved into arr.

Should I use std::initializer_list, or std::vector, or something else?

Aucun commentaire:

Enregistrer un commentaire