typedef

Hi all,
 
typedef int Int;

Now, Int has the same meaning as int. So the compiler gives the second element behind typedef ( Int ) the meaning of the first element ( int ). But why then doesn't this work:
 
typedef char[ 260 ] string;

and does this work:
 
typedef char string[ 260 ];


It seems to me that it is more logical vice versa, because string now means a char array of 260 elements.

Thanks!
Last edited on
The syntax of a typedef is the same as the syntax of an object declaration, except that the keyword 'typedef' is added in the beginning:
1
2
3
4
        int Int; // object called Int, of type int
typedef int Int; // type called Int, alias of int
        char string[ 260 ]; // object called "string", of type char[260]
typedef char string[ 260 ]; // type called "string", alias of char[260] 

if you want to use a more intuitive syntax, C++ has one (since last year)
1
2
using Int = int;
using string = char[260];

http://liveworkspace.org/code/ce2451c85230b16d781bada60c493ba6
Thanks!
closed account (zb0S216C)
@Cubbi: That's something new to me. Has that "using" declaration been standardised? Also, where can I find more information on it?

Wazzak
Last edited on
Framework wrote:
Has that "using" declaration been standardised?

Yes, of course, it's under ยง7.1.3[dcl.typedef]/2 in C++11, the paragraph that starts with "A typedef-name can also be introduced by an alias-declaration"

Also, where can I find more information on it?

There's a little bit on http://en.cppreference.com/w/cpp/language/type_alias , but that's still a todo item there, like most of the core language. Stroustrup has a bit at http://www.stroustrup.com/C++11FAQ.html#template-alias too (see last paragraph of that chapter). It's just a little convenience improvement.
Last edited on
closed account (zb0S216C)
Thanks for the information, Cubbi :) I appreciate it.

Wazzak
Topic archived. No new replies allowed.