11

I'm using struct with initialized value inside a struct, like below example. What I can't understand is why the compiler can't set size of char[] = { .. } in a struct member like it does for a variable declaration? Clang++ gives the following error:

error: array bound cannot be deduced from an in-class initializer

Code:

struct A
{
    char s[]   = { 'F', 'O', 'O', ... };
};
dyp
  • 38,334
  • 13
  • 112
  • 177
The Mask
  • 17,007
  • 37
  • 111
  • 185
  • `[C++11: 8.3.4/1]`/`[dcl.array]` tells us that this syntax sometimes isn't valid though, funnily enough, I can't find the actual wording prohibiting it in a _member-declaration_. – Lightness Races in Orbit Mar 19 '14 at 15:10
  • 1
    Actually not an unfair question. The simple answer is obviously "because the standard disallows it", but not only can I not immediately find this text (I'm sure it's there somewhere, probably horrendously indirectly), but the reason why that rule should be in place also eludes me. – Lightness Races in Orbit Mar 19 '14 at 15:12
  • A similar problem occurs for `auto` btw: It's allowed for `static` data members, but not for non-static. – dyp Mar 19 '14 at 15:13
  • I guess the reason why `auto` is forbidden is that name lookup inside braced-or-equal-initializers for non-static data members is postponed until after the class definition (like name lookup inside the body of a member function). Maybe a similar reasoning applies to this case (see, e.g. [class.mem]/2) – dyp Mar 19 '14 at 15:24
  • The first question to ask is, can you have an in-class initializer for an array at all? – ach Mar 20 '14 at 08:35
  • 1
    What are dots supposed to do? – BЈовић Mar 20 '14 at 10:06

2 Answers2

0

Structure elements cannot be initialized while they're created, because the structure defines a type and not a variable which can be initialized. You can only initialize a variable of a structure after creating an object of a structure

for example

struct test
{
    int var;
};

struct test struct_object = { 7 };
Sorcrer
  • 1,634
  • 1
  • 14
  • 27
  • 6
    That is correct for C++98 and C++03, but not C++11. You _can_ initialize a variable in a structure. That will be equivalent to having it appear in the constructor's initializer list. The question does not explicitly say "C++11" but assumes it by the wording "like it does for a variable". – Damon Mar 20 '14 at 11:08
  • I added the C++11 tag to the question: Clang++ emits this error message (quoted in the OP) only in C++11 mode. – dyp Mar 20 '14 at 11:26
  • It's C++11. Sorry for not tag before. – The Mask Mar 20 '14 at 12:54
0

My best guess is it has to do with the passes the compiler is making. In the first pass the structure is initialized, this means it's size MUST be assigned at this time, so if forces the array to size [0]. Then on a later pass when it does assignment it can't store into the size [0] array and it fails to compile. Try giving a size and this should be a good alternative:

#include <iostream>
using namespace std;

struct myStruct{
    char myArray[4] = {'1','2','3','\0'};

};

int main()
{
    myStruct Mystructure;
    cout<<Mystructure.myArray;

    return 0;
}

Another solution would be to do it in the base member initialization list for the constructor(which would be required in this case), but I'm not sure how to do that with an array. I would think it would work though as it seems to be a similar reason to why you can't initialize constants if I had to guess.

Firestar9114
  • 432
  • 4
  • 9
  • It's not simply a compile-order limitation, there's a very good reason which is explained at http://stackoverflow.com/questions/29593207/what-is-the-reason-for-not-being-able-to-deduce-array-size-from-initializer-stri?noredirect=1&lq=1#comment47333319_29593207 – Ben Voigt Apr 21 '17 at 15:29
  • I don't understand that, why wouldn't it just get the size from the base member initialization list since it's run first? How is it confusing to implement that? – Firestar9114 Apr 21 '17 at 21:24
  • From *which* ctor-initializer-list? Every constructor has its own. – Ben Voigt Apr 22 '17 at 03:30