2

I was trying to create a std::array of char array so that I could declare it static constexpr.

eg:

#include <array>

int main(){

  static constexpr char A[] = "abc";
  static constexpr char B[] = "def";
  static constexpr std::array<char[], 3> tmp{A, B};
}

When I do this I get error message " too many initializers for"

6
  • There is no std::array in your code Commented Feb 9, 2018 at 14:58
  • Might be a duplicate of stackoverflow.com/q/26629609/10077. Commented Feb 9, 2018 at 14:58
  • @tkausl updated Commented Feb 9, 2018 at 14:59
  • @DanielLangr Sorry it was supposed to be A and B. Commented Feb 9, 2018 at 15:01
  • char[] here seem to be an invalid use for std::array Commented Feb 9, 2018 at 15:05

3 Answers 3

1

By output of your compiler I could surmise that you have a non-standard extension active. Stricly ISO-wise use of char[] as parameter of template is not legal,. Some compilers would treat that as char[0].

what your array meant to store? way you're trying to do it would store adresses of array to char (and only way to do so would be replace char[] by by const char* in template parameters. std::array is trivial class which stores data statically so you cannot implement array with variable length of strings that way.

Either 1) abolish constexpr and use std::string 2) use const char* 3) worst case (which sometimes is the best) - use fixed array length or "naked" 2-dimensional array with aggregate initializer list.

Sign up to request clarification or add additional context in comments.

2 Comments

When I said it to store char[] why is std::array trying to store address? shouldn't it store char[]?
@mike depends on which compiler you use. judging by output of yours it was treating char[] as char[0] and is unable to store literals you give it. One I used was saying that char[] is illegal, and it is by ISO standard. so 1) store addresses 2) store string classes 3) store arrays of fixed length. actually can use std::array instead of char[x]
1

With C++20 std::to_array can be used.

static constexpr char A[] = "abc";
static constexpr char B[] = "def";
static constexpr auto tmp = std::to_array({A, B});

Comments

0

You may use:

static constexpr std::array<char[4], 2> tmp {{{ A[0], A[1], A[2], A[3] },
                                              { B[0], B[1], B[2], B[3] }}};

or

static constexpr std::array<char[4], 2> tmp { "abc", "def" };

From http://en.cppreference.com/w/cpp/container/array:

When initializing an object of array type, the initializer must be either a string literal (optionally enclosed in braces) or be a brace-enclosed list of initialized for array members.

Thus you cannot initialize an array (member of std::array of char[4]) by an object of another array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.