typedef an array

1,503 views
Skip to first unread message

chub...@gmail.com

unread,
Dec 3, 2012, 9:58:59 PM12/3/12
to std-dis...@isocpp.org
The typedef of an array of 20 integers is written as

typedef int INTBUF[20];

instead of 

typedef int[20] INTBUF;

I would like to know if there is a reason for this as I find the second one is more natural than the first one.

J. Daniel Garcia

unread,
Dec 3, 2012, 10:19:44 PM12/3/12
to std-dis...@isocpp.org
Without going into details, I would say C compatibility.

BTW, in C++11 you can do:

using IntBuf = int[20];

which I think is clear and avoids confussion.


--
 
 
 

Tony V E

unread,
Dec 4, 2012, 2:11:27 AM12/4/12
to std-dis...@isocpp.org
Is there any reason why we can't add:

int[20] foo;

As valid syntax?
Or would that start to collide with lambda syntax in some situations? 

Tony

Sent from my portable Analytical Engine


From: "J. Daniel Garcia" <josedani...@uc3m.es>
To: "std-dis...@isocpp.org" <std-dis...@isocpp.org>
Sent: 3 December, 2012 10:20 PM
Subject: Re: [std-discussion] typedef an array
--
 
 
 

Nevin Liber

unread,
Dec 4, 2012, 2:37:56 AM12/4/12
to std-dis...@isocpp.org
On 4 December 2012 01:11, Tony V E <tvan...@gmail.com> wrote:
Is there any reason why we can't add:

int[20] foo;

First off, what reasons do you have for adding it?
--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com(847) 691-1404

Tony V E

unread,
Dec 4, 2012, 2:49:07 AM12/4/12
to std-dis...@isocpp.org



From: "Nevin Liber" <ne...@eviloverlord.com>
To: "std-dis...@isocpp.org" <std-dis...@isocpp.org>
Sent: 4 December, 2012 2:43 AM

Subject: Re: [std-discussion] typedef an array
--
 
It is more consistent, easier to understand, easier in complicated cases like the return from a function, etc.
The typical reasons for a feature that is not new functionality.

Tony
 

Jean-Marc Bourguet

unread,
Dec 4, 2012, 3:22:40 AM12/4/12
to std-dis...@isocpp.org
You define a variable which is of type array of 20 integers as

int name[20];

you define a typedef instead of a variable by prepending typedef, so

typedef int name[20];


If it is the reason for whole syntax of definitions of variables that
you want
to know, the principle is that it mimic the use. If you know how to
use a variable
for a given type, you know how to define it.

Yours,

--
Jean-Marc

Jean-Marc Bourguet

unread,
Dec 4, 2012, 3:31:15 AM12/4/12
to std-dis...@isocpp.org
On Tue, 04 Dec 2012 07:49:07 -0000, Tony V E <tvan...@gmail.com>
wrote:

> It is more consistent, easier to understand, easier in complicated
> cases like the return from a function, etc.
> The typical reasons for a feature that is not new functionality.

More consistent is a case which need to be done. I'm not in love with
the "definition mimic the use" syntax, but it is consistent and adding
a redundant syntax for something already available seems to me adding
to the confusion.

Yours,

--
Jean-Marc

Nicol Bolas

unread,
Dec 4, 2012, 3:35:53 AM12/4/12
to std-dis...@isocpp.org

I don't know about that. How was this variable defined:

unknown[5] = 10;

This could be one of many things, but let's only consider basic types. It could have been as an array of at least 6 elements. Or it could have been as a pointer. You don't know just from how a variable is used how to define it.

So I would say that this argument doesn't really work.

Ultimately, I would say that if there were a proposal for allowing `int[20]` to be a full-fledged type, it should also allow for function pointer definitions to be able to be defined similarly. Otherwise, it's not enough of a fix.

Nevin Liber

unread,
Dec 4, 2012, 3:40:16 AM12/4/12
to std-dis...@isocpp.org
On 4 December 2012 01:49, Tony V E <tvan...@gmail.com> wrote:
It is more consistent, easier to understand, easier in complicated cases like the return from a function, etc.

Um, you can't return an array from a function..
-- 

Tony V E

unread,
Dec 4, 2012, 4:40:15 AM12/4/12
to std-dis...@isocpp.org
Sorry, right.  But you can do reference to an array.  I was thinking of some of those ugly cases in the various countof templates.


Sent from my portable Analytical Engine

Sent: 4 December, 2012 3:40 AM

Subject: Re: [std-discussion] typedef an array
--
 
 
 

Johannes Schaub

unread,
Dec 4, 2012, 4:47:39 AM12/4/12
to std-dis...@isocpp.org


Am 04.12.2012 10:40 schrieb "Tony V E" <tvan...@gmail.com>:
>
> Sorry, right.  But you can do reference to an array.  I was thinking of some of those ugly cases in the various countof templates.
>

This is taken care of with alias templates

    template<typename T> using id = T;

So you can say

    id<char const [3]> &x = "12";

>
> Sent from my portable Analytical Engine
>

Sent from my portable android device.

> ________________________________
> From: "Nevin Liber" <ne...@eviloverlord.com>
> To: "std-dis...@isocpp.org" <std-dis...@isocpp.org>
> Sent: 4 December, 2012 3:40 AM
>
> Subject: Re: [std-discussion] typedef an array
>
> On 4 December 2012 01:49, Tony V E <tvan...@gmail.com> wrote:
>>
>> It is more consistent, easier to understand, easier in complicated cases like the return from a function, etc.
>
>
> Um, you can't return an array from a function..
> -- 
>  Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com(847) 691-1404
>
> --
>  
>  
>  
>

> --
>  
>  
>  

Tony V E

unread,
Dec 4, 2012, 11:10:43 PM12/4/12
to std-dis...@isocpp.org
Maybe I was being to curt and cryptic.  The original question was about typedef, I kind of went off on a tangent. Basically, I find many new users get hung up on declaring arrays. They expect this to work:

int[20] foo;

And stumble over:

int foo[20];

I can live (and obviously have lived) with the latter, but I was just wondering if there were any obvious obstacles to making the former also valid syntax.

I don't think an id<> template would solve the problem of making it easier to understand. 

I think you could also extend the argument to other unwieldy C/C++ declarations, like pointers to functions, etc.  Ie basically get all the type information on the left and the variable name on the right. 

TYPE varName;

Instead of

TYPESTUFF varName MORETYPESTUFF;

 We can (and often do) use typedef to achieve that, but why should we have to?  I think typedef should be used for a bit of abstraction (ie this thing is a handle_t, ignore that it is an int), not just a workaround for awkwardness. 

So, I'm more interested in obvious obstacles than whether anyone thinks it is worthwhile, but I'll take whatever opinions you have.

Personally, it has always seemed odd to me that a variables type might be int[20], but I can't type it that way.

Tony

Sent from my portable Analytical Engine


From: "Johannes Schaub" <schaub....@googlemail.com>
To: "std-dis...@isocpp.org" <std-dis...@isocpp.org>
Sent: 4 December, 2012 4:47 AM

Subject: Re: [std-discussion] typedef an array


Am 04.12.2012 10:40 schrieb "Tony V E" <tvan...@gmail.com>:
>
> Sorry, right.  But you can do reference to an array.  I was thinking of some of those ugly cases in the various countof templates.
>

This is taken care of with alias templates

    template<typename T> using id = T;

So you can say

    id<char const [3]> &x = "12";

>
> Sent from my portable Analytical Engine
>

Sent from my portable android device.

> ________________________________
> From: "Nevin Liber" <ne...@eviloverlord.com>
> To: "std-dis...@isocpp.org" <std-dis...@isocpp.org>
> Sent: 4 December, 2012 3:40 AM
>
> Subject: Re: [std-discussion] typedef an array
>
> On 4 December 2012 01:49, Tony V E <tvan...@gmail.com> wrote:
>>
>> It is more consistent, easier to understand, easier in complicated cases like the return from a function, etc.
>
>
> Um, you can't return an array from a function..
> -- 
>  Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com(847) 691-1404
>
> --
>  
>  
>  
>
> --
>  
>  
>  

--
 
 
 

Steve Lorimer

unread,
Dec 4, 2012, 11:53:55 PM12/4/12
to std-dis...@isocpp.org
Slightly off topic, but for what it's worth I've always thought it odd that for typedefs we do:

    typedef fully_qualified_type my_new_type;

and then for namespace aliases we do:

    using namespace my_new_namespace = fully_qualified_namespace;


Nevin Liber

unread,
Dec 5, 2012, 1:23:04 AM12/5/12
to std-dis...@isocpp.org
On 4 December 2012 22:10, Tony V E <tvan...@gmail.com> wrote:
Maybe I was being to curt and cryptic.  The original question was about typedef, I kind of went off on a tangent. Basically, I find many new users get hung up on declaring arrays. They expect this to work:

int[20] foo;

And stumble over:

int foo[20];

If they are beginners, why aren't they using:

std::array<int, 20>?


I think you could also extend the argument to other unwieldy C/C++ declarations, like pointers to functions, etc.

Then why not propose it to the C Committee?


Every feature added has a cost.  Even though the intent is to help code writers, it makes things more complicated for code readers, compiler writers, book writers and teachers.

Just adding a divergence from C isn't worth that cost (to me).  If it added some additional expressive power, I might think differently.

Chris Jefferson

unread,
Dec 5, 2012, 4:51:55 AM12/5/12
to std-dis...@isocpp.org
It is very odd!

That's why in C++11 you can write:

using my_new_type = fully_qualified_type;

and even

using array_def = int[10];


Part of the reason for this new notation was to get away from the horrible strangeness of typedefs. I have sympathy for people who would like to try to make typedef better, but I wonder if the better thing to do in C++ is just to move people to 'using'.

Chris

chub...@gmail.com

unread,
Dec 5, 2012, 9:55:39 PM12/5/12
to std-dis...@isocpp.org
Sure. Then it would be a lot more consistent to have the typedef syntax also accomodate this thought process in addition to the current syntax
Essentially, typedef int[20] BUF; and typedef int BUF[20] can both mean a type alias for an integer buffer of 20 elements.

Sebastian Gesemann

unread,
Dec 6, 2012, 8:33:25 AM12/6/12
to std-dis...@isocpp.org
I hope that you don't intend to propose special casing the declaration
syntax just for typedefs. If the declaration syntax is changed, this
change should apply consistently over the complete language and not
just typedefs.

A declaration has two parts: the type-specifier and the declarator.
But the type specifier is NOT int[20] in this case. It's just "int".
The declarator includes the name you're declaring/typedef'ing as well
as all "operators" and such, in this case: INTBUF[20].

If you want

typedef int[20] INTBUF;

to work, you're really using [20] as a "prefix operator" in the declarator:

typedef int [20]INFBUF;
^^^ ^^^^^^^^^^
| |
type-specifier |
|
declarator

But allowing this opens up ambiguity issues. For example, what does
the following line

using what_am_i = int[2][3];

declare what_am_i to be? Are these the postfix brackets or the prefix
brackets? What would a corresponding typedef look like?

typedef int [2][3]what_am_i;
typedef int what_am_i[2][3];

Is what_am_i an array of three arrays of two ints?
Is what_am_i an array of two arrays of three ints?

The brackets are not the only "postfix declaration operator". For
example, we declare functions like this

int bar(int x, double y);

and not like this

int (int x, double y) bar;

Instead of making the declaration syntax more complicated and deal
with ambiguity issues, you could simply use the new type alias syntax:

using INTBUF = int[20];
using bar_type = int(int,double);

In my opinion, figuring out how

typedef int[20] INTBUF;

could be made to work without breaking something is not worth the
hassle. If you feel strongly about this, I suggest that you dig deeper
into the grammar and/or try to implement this in clang without dirty
hacks.

chub...@gmail.com

unread,
Dec 6, 2012, 10:40:19 PM12/6/12
to std-dis...@isocpp.org
That would be an interesting addition in my TBD list
Reply all
Reply to author
Forward
0 new messages