Why does array size initialization differ inside or outside a class/struct/...?

Steve Van Opstal

The first example will automagically know it's size by the elements it is initialized with. The second example requires you to explicitly define the length of the array (to get it working). Why is this the case? Is there a reason behind this?

Array initialization outside a class/struct/...

const int intArray[] = { 0, 1, 2 };

struct Example
{
    int SizeOfArray()
    {
        return sizeof(intArray);
    }
};

Array initialization inside a class/struct/...

Faulting

struct Example
{
    const int intArray[] = { 0, 1, 2 };

    int SizeOfArray()
    {
        return sizeof(intArray);
    }
};

Error: cannot specify explicit initializer for arrays

Solution

struct Example
{
    const int intArray[3] = { 0, 1, 2 };

    int SizeOfArray()
    {
        return sizeof(intArray);
    }
};
xaizek

I think it's because in-class/struct initialization is just syntactic sugar for writing part of initializer list in a different place. According to this, your faulting example is equal to the following:

struct Example
{
    const int intArray[];

    Example() :
       // start of compiler-generated pseudo code
       intArray[0](0),
       intArray[1](1),
       intArray[2](2)
       // end of compiler-generated pseudo code
    {
    }

    int SizeOfArray()
    {
       return sizeof(intArray);
    }
};

The code above obviously defines an incomplete structure of unknown size, just as @harper said in his comment.

P.S. Couldn't find proof in the standard, but I'm pretty sure that it covers this case, probably implicitly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does array size initialization differ inside or outside a class/struct/...?

From Dev

Why does array initialization always resort to int?

From Dev

Java Array Size Initialization

From Dev

Why does the structure size differ in 32-bit and 64-bit program?

From Dev

Why does the structure size differ in 32-bit and 64-bit program?

From Dev

Why does the function signature differ between array_map and array_filter/array_reduce?

From Dev

Why does the priority from getPriority inside a new thread differ from the priority in the calling thread?

From Dev

Why does mutating a passed Option inside a function not propagate to the Option outside?

From Dev

Javascript: Why array variable assignment is behaving differently inside and outside this function?

From Dev

Why do inherited Docker images differ in size

From Dev

Why does this template for an array's size not work?

From Dev

Why does static initialization of flexible array member work?

From Dev

Does anyone know why this array initialization statement won't work?

From Dev

Why does 'selector' return value differ from 'this'

From Dev

Why does the result in TOAD and SQLPlus differ?

From Dev

Why does my output differ for this code snippet?

From Dev

Why does UINavigationController have an unexpected size inside a container view?

From Dev

Why does the method add() does not increase the size of the array?

From Dev

Why does the compiler not optimize this initialization?

From Dev

const static member initialization - inside vs outside class definition

From Dev

Why is array initialization in a static context?

From Dev

why does swift dictionary with function work outside of a class but produces error inside of a class?

From Dev

Why does this D3 code add the <p> element outside the body, instead of inside it?

From Dev

Why does anchor outside H2 tag work and not inside a H2 header tag

From Dev

Why does 2 dimension array passing to function requires its size?

From Dev

Why does array-size declaration use "1" as the first index?

From Dev

Variable size array as class member: why does it compile?

From Dev

C++: Why does int array[size] work?

From Dev

Why does 2 dimension array passing to function requires its size?

Related Related

  1. 1

    Why does array size initialization differ inside or outside a class/struct/...?

  2. 2

    Why does array initialization always resort to int?

  3. 3

    Java Array Size Initialization

  4. 4

    Why does the structure size differ in 32-bit and 64-bit program?

  5. 5

    Why does the structure size differ in 32-bit and 64-bit program?

  6. 6

    Why does the function signature differ between array_map and array_filter/array_reduce?

  7. 7

    Why does the priority from getPriority inside a new thread differ from the priority in the calling thread?

  8. 8

    Why does mutating a passed Option inside a function not propagate to the Option outside?

  9. 9

    Javascript: Why array variable assignment is behaving differently inside and outside this function?

  10. 10

    Why do inherited Docker images differ in size

  11. 11

    Why does this template for an array's size not work?

  12. 12

    Why does static initialization of flexible array member work?

  13. 13

    Does anyone know why this array initialization statement won't work?

  14. 14

    Why does 'selector' return value differ from 'this'

  15. 15

    Why does the result in TOAD and SQLPlus differ?

  16. 16

    Why does my output differ for this code snippet?

  17. 17

    Why does UINavigationController have an unexpected size inside a container view?

  18. 18

    Why does the method add() does not increase the size of the array?

  19. 19

    Why does the compiler not optimize this initialization?

  20. 20

    const static member initialization - inside vs outside class definition

  21. 21

    Why is array initialization in a static context?

  22. 22

    why does swift dictionary with function work outside of a class but produces error inside of a class?

  23. 23

    Why does this D3 code add the <p> element outside the body, instead of inside it?

  24. 24

    Why does anchor outside H2 tag work and not inside a H2 header tag

  25. 25

    Why does 2 dimension array passing to function requires its size?

  26. 26

    Why does array-size declaration use "1" as the first index?

  27. 27

    Variable size array as class member: why does it compile?

  28. 28

    C++: Why does int array[size] work?

  29. 29

    Why does 2 dimension array passing to function requires its size?

HotTag

Archive