typedef with array without size

Quick question for anyone that knows... is the following standard C++? (I know, it might as well be C)

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef unsigned long Foo[ ], *PFoo;

int main()
{
    {
        Foo foo = {3, 4, 2};
        PFoo pFoo = foo;
    }
    {
        Foo foo = {3, 4, 2, 5};
        PFoo pFoo = foo;
    }
}


Specifically, I'm asking if typedefs with an array require the array to have a size?
e.g. typedef unsigned long Foo[3];
Or is this some sort of non-standard extension?

___________________________________________

cppreference only shows an example with a fixed-size,
https://en.cppreference.com/w/cpp/language/typedef
typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
Last edited on
-pedantic will tell you if there is anything that is non conforming.

the syntax being used is int items[] = {1,2,3};
No warnings on pedantic. But pedantic also doesn't warn of VLAs, so I'm not totally convinced yet that it's legal.

Edit: I amend my statement -- cpp.sh doesn't show the VLA warning with int a = 3; int arr[a];,
but my desktop g++ compiler does.

The reason I am still not exactly convinced is that cppreference shows the unsized typedef in its C documentation, but not in its C++ documentation, leading me to believe it isn't allowed C++.
Last edited on
But pedantic also doesn't warn of VLAs

-pedantic warns of VLAs in both g++ and clang++.
Well, I think I found my answer,
Cplusplus17 standard wrote:
After all adjustments of types (during which typedefs (10.1.3) are replaced by their definitions), the types
specified by all declarations referring to a given variable or function shall be identical, except that declarations
for an array object can specify array types that differ by the presence or absence of a major array bound
(11.3.4). A violation of this rule on type identity does not require a diagnostic.

And later on...
...if the constant expression is omitted, the type of the identifier of D is “derived-declarator-type-list array of unknown bound of T”, an incomplete object type.

So yes, it sounds like it's legal, but of course the type is incomplete, so it cannot be used in sizeof(Foo), for instance.
Last edited on
> if typedefs with an array require the array to have a size?

We can have a typedef (or type alias) for an incomplete type (array of unknown bound).

For example:
1
2
3
4
5
6
7
8
9
10
11
using a1_t = int[] ; // alias for array of unknown bound
extern int a1[] ; // declaration of array of unknown bound
extern a1_t a11 ; // declaration of array of unknown bound

struct X ; // X is an incomplete type at this point

using a2_t = X[100] ; // alias for array of an incomplete type (known bound)
using a3_t = X[] ; // alias for array of unknown bound of an incomplete type

extern a2_t a21 ; //declaration array of an incomplete type (known bound)
extern X a2[] ; // declaration of array of unknown bound of an incomplete type 
Makes sense, thanks for the examples.
Topic archived. No new replies allowed.