1
static_assert(0<decltype(AType::Id)::MaxIdLen);
using __type = char[decltype(AType::Id):: MaxIdLen + 10000];
[[maybe_unused]] __type aa = ""; //error  initializer-string for char array is too long

I am getting this weird compiler error saying the initializer string is too long. But it is actually not. The first static_assert passed.

Has anyone seen such an issue before? I am using clang.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    Unrelated to your problem, but please read [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) Using leading double underscore in `__type` is not allowed. – Some programmer dude Nov 02 '21 at 06:57
  • Just how big is `MaxIdLen`? I need to use around `1000000000` to get that error –  Nov 02 '21 at 07:02
  • 2
    As for the error you get, first of all please try to create a proper [mre] to show us. Then copy-paste the *full* and *complete* error output you get, as text, info your question. – Some programmer dude Nov 02 '21 at 07:03

1 Answers1

1

... compiler error saying the initializer string is too long. But it is actually not.

The compiler is not telling you that the string is too long to init aa. It's telling you that it's more data than it can handle, presumably because decltype(AType::Id)::MaxIdLen is a large value.

The compiler doesn't just store the string literal in the character array. It initialises the entire character array by using the string literal as a prefix, and padding the rest with zeros.