1

How can on assign something to a std::vector<char[8]>?

#include <vector>
void test(void)
{
 std::vector<char[8]> x; // works

 x.push_back("7777777");
 //
 // error: array 'new' cannot have initialization arguments
 // error: object expression of non-scalar type 'char [8]' 
 //        cannot be used in a pseudo-destructor expression

 char a[8] = "7777777";  
 x.push_back(a);  
 //
 // error: array 'new' cannot have initialization arguments
 // error: object expression of non-scalar type 'char [8]' 
 //        cannot be used in a pseudo-destructor expression

 char b[2][8] = {"7777777", "8888888"};
 x.assign(b, b+2);
 // 
 // error: static_assert failed "type is not assignable"
 // error: array 'new' cannot have initialization arguments

}// end of function

It looks like one can create a std::vector<char[8]> but it is not possible to assign anything to it. How can one make at least the last line (i.e. x.assign(b, b+2)) to work?

Thank you very much for your help!

S.V
  • 2,149
  • 2
  • 18
  • 41
  • 2
    `std::vector x;` – NathanOliver Oct 14 '19 at 16:21
  • 1
    Arrays are very dumb. They are an elegant solution to the problems of the 60s and 70s, but today they cracks show. Since you are storing strings, consider `std::string`. If that has already been eliminated, consider using a `std::array` – user4581301 Oct 14 '19 at 16:22
  • Thank you! This is an obvious solution, of course. All of my strings are 8 character long, and so I thought that using `char[8]` would be more efficient.Is it possible or the `std::string` route is the only way to go? – S.V Oct 14 '19 at 16:23
  • May I recommend : [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list#388282). – Jesper Juhl Oct 14 '19 at 16:23
  • 2
    @S.V `std::vector x;` would be the most natural replacement. If you find through profiling and benchmarking that it is to slow, you could use a `std::vector> x;` but you can't use string literals with that. There is also `std::vector x;` but that comes with object lifetime concerns. – NathanOliver Oct 14 '19 at 16:26

1 Answers1

5

You can'ṭ have a std::vector<char[8]> because char[8] does not satisfy the requirements of std::vector for the value type. In praticular, the value type is required to be Erasable, which char[8] is not given the default allocator.

You can use std::vector<std::array<char,8>> instead. std::array<char,8> is not only Erasable, but is also assignable.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Somehow, `std::vector> y; char c[2][8] = {"7777777", "8888888"}; y.assign(c, c+2);` does not work. I get the following errors: `error: no viable overloaded '='` and `error: no matching constructor for initialization of 'std::array'`. While, `std::vector` seems to work. – S.V Oct 14 '19 at 16:54
  • @S.V `std::array` cannot be created from a string literal like a normal array can. You can use a helper function such as shown here: https://stackoverflow.com/a/33484394/2079303 – eerorika Oct 14 '19 at 16:59