Connecting Tech Pros Worldwide Help | Site Map

typedef question

Alex
Guest
 
Posts: n/a
#1: Nov 28 '05
Hello people,

I am getting errors from VS2003 when working with typedef'ed types.

For example, assume that I have a type T, defined in a 3rd party include file based on some condition

#if (condition)
typedef char T;
#else
typedef short T;
#endif

Let's assume, for the sake of discussion that the condition is true. So we get:

typedef char T;

Now, I want to use the unsigned form of T in my code:

unsigned T t;

This gives me the following errors:

error C2628: 'T' followed by 'unsigned' is illegal (did you forget a ';'?)

When instead I try:

T unsigned t;

I get:

error C2146: syntax error : missing ';' before identifier 't'
error C2377: 'T' : redefinition; typedef cannot be overloaded with any other symbol. see declaration of 'T'
error C2065: 't' : undeclared identifier


What am I doing wrong?


Best wishes,
Alex.

--
Address email to user "response" at domain "alexoren" with suffix "com"
Howard
Guest
 
Posts: n/a
#2: Nov 28 '05

re: typedef question



"Alex" <response@myrealbox.com> wrote in message
news:1133199549.74f1542c92a2054036802cce04b041aa@n ews.nntpserver.com...
[color=blue]
> For example, assume that I have a type T, defined in a 3rd party include
> file based on some condition
>
> #if (condition)
> typedef char T;
> #else
> typedef short T;
> #endif
>
> Let's assume, for the sake of discussion that the condition is true. So
> we get:
> typedef char T;
>
> Now, I want to use the unsigned form of T in my code:
> unsigned T t;
>
> This gives me the following errors:
> error C2628: 'T' followed by 'unsigned' is illegal (did you forget a
> ';'?)
>
> When instead I try:
> T unsigned t;
>
> I get:
> error C2146: syntax error : missing ';' before identifier 't'
> error C2377: 'T' : redefinition; typedef cannot be overloaded with any
> other symbol. see declaration > of 'T'
> error C2065: 't' : undeclared identifier
>
> What am I doing wrong?
>[/color]

I don't think you are allowed to use typedef's in that manner. If instead
you were using a #define'd symbol, then adding "unsigned" in front of it
would be ok (assuming of course that the type you were using is valid when
qualified with unsigned). But a typedef defines a type, not just a symbol.
It's as if you declared a class CMyClass, and then tried to use "unsigned
CMyClass". That just doesn't make sense.

If you need an unsigned char or unsigned short, based on "condition", then
you'll need to make a similar set of typedef's for the unsigned versions.

-Howard




Victor Bazarov
Guest
 
Posts: n/a
#3: Nov 28 '05

re: typedef question


Alex wrote:[color=blue]
> Hello people,
>
> I am getting errors from VS2003 when working with typedef'ed types.
>
> For example, assume that I have a type T, defined in a 3rd party include file based on some condition
>
> #if (condition)
> typedef char T;
> #else
> typedef short T;
> #endif
>
> Let's assume, for the sake of discussion that the condition is true. So we get:
>
> typedef char T;
>
> Now, I want to use the unsigned form of T in my code:
>
> unsigned T t;
>
> This gives me the following errors:
>
> error C2628: 'T' followed by 'unsigned' is illegal (did you forget a ';'?)
>
> When instead I try:
>
> T unsigned t;
>
> I get:
>
> error C2146: syntax error : missing ';' before identifier 't'
> error C2377: 'T' : redefinition; typedef cannot be overloaded with any other symbol. see declaration of 'T'
> error C2065: 't' : undeclared identifier
>
>
> What am I doing wrong?[/color]

T is not a macro. It's a typedef-id. You cannot combine it with anything
except 'static', 'auto', 'register', 'const', 'volatile', or 'const
volatile'. IOW, since 'unsigned' is _not_ one of linkage specifiers or
cv-qualifiers, or storage specifiers, you cannot add it. Modify your code
to have another term for unsigned T:

#if (condition)
typedef char T
typedef unsigned char UT
#else
typedef short T
typedef unsigned short UT
#endif

and use "UT" instead of "unsigned T".

V
Gianni Mariani
Guest
 
Posts: n/a
#4: Nov 28 '05

re: typedef question


Alex wrote:[color=blue]
> Hello people,
>
> I am getting errors from VS2003 when working with typedef'ed types.
>
> For example, assume that I have a type T, defined in a 3rd party include file based on some condition
>
> #if (condition)
> typedef char T;
> #else
> typedef short T;
> #endif
>
> Let's assume, for the sake of discussion that the condition is true. So we get:
>
> typedef char T;
>
> Now, I want to use the unsigned form of T in my code:
>
> unsigned T t;
>
> This gives me the following errors:
>
> error C2628: 'T' followed by 'unsigned' is illegal (did you forget a ';'?)
>
> When instead I try:
>
> T unsigned t;
>
> I get:
>
> error C2146: syntax error : missing ';' before identifier 't'
> error C2377: 'T' : redefinition; typedef cannot be overloaded with any other symbol. see declaration of 'T'
> error C2065: 't' : undeclared identifier
>
>
> What am I doing wrong?[/color]

I would do this differently. First I would use a template class like:

template <typename T>
struct Type
{
typedef T t_signed;
typedef unsigned T t_unsigned;
};


typedef Type< ifelse<condition,char,short>::type > AppType;

Now, in your code you can use:

AppType::t_signed t;

and

AppType::unsigned t;

To your hearts content. Note - NO MACROS.

It's also easy to extend and specialize as needed.

Note - ifelse is somthing like

template <bool condition, typename T1, typename T2>
struct ifelse
{
typedef T1 type;
};

template <typename T1, typename T2>
struct ifelse<false, T1, T2>
{
typedef T2 type;
};

Warning - This is not compiled code but a brain dump, you will find
errors. VS2003 should easily handle this.

Kai-Uwe Bux
Guest
 
Posts: n/a
#5: Nov 28 '05

re: typedef question


Gianni Mariani wrote:
[color=blue]
> Alex wrote:[color=green]
>> Hello people,
>>
>> I am getting errors from VS2003 when working with typedef'ed types.
>>
>> For example, assume that I have a type T, defined in a 3rd party include
>> file based on some condition
>>
>> #if (condition)
>> typedef char T;
>> #else
>> typedef short T;
>> #endif
>>
>> Let's assume, for the sake of discussion that the condition is true. So
>> we get:
>>
>> typedef char T;
>>
>> Now, I want to use the unsigned form of T in my code:
>>
>> unsigned T t;
>>
>> This gives me the following errors:
>>
>> error C2628: 'T' followed by 'unsigned' is illegal (did you forget a
>> ';'?)
>>
>> When instead I try:
>>
>> T unsigned t;
>>
>> I get:
>>
>> error C2146: syntax error : missing ';' before identifier 't'
>> error C2377: 'T' : redefinition; typedef cannot be overloaded with
>> any other symbol. see declaration of 'T' error C2065: 't' :
>> undeclared identifier
>>
>>
>> What am I doing wrong?[/color]
>
> I would do this differently. First I would use a template class like:
>
> template <typename T>
> struct Type
> {
> typedef T t_signed;
> typedef unsigned T t_unsigned;[/color]

This will not fly: unlike const or volatile, unsigned is not a qualifier. It
cannot be stripped of or added by a template in a straight forward way. If
you want a template to yield the (un)signed version of a type, you need to
do a bunch of partial specializations like, for instance:


namespace DO_NOT_USE {

template < typename T >
struct signed_type {

typedef T the_type;

}; // signed_type

template <>
struct signed_type< unsigned char > {

typedef signed char the_type;

};

template <>
struct signed_type< char > {

typedef signed char the_type;

};

template <>
struct signed_type< unsigned short > {

typedef short the_type;

};

template <>
struct signed_type< unsigned int > {

typedef int the_type;

};

template <>
struct signed_type< unsigned long > {

typedef long the_type;

};

template < typename T >
struct unsigned_type {

typedef T the_type;

}; // unsigned_type

template <>
struct unsigned_type< char > {

typedef unsigened char the_type;

};

template <>
struct unsigned_type< signed char > {

typedef unsigened char the_type;

};

template <>
struct unsigned_type< signed short > {

typedef unsigned short the_type;

};

template <>
struct unsigned_type< signed int > {

typedef unsigned int the_type;

};

template <>
struct unsigned_type< signed long > {

typedef unsigned long the_type;

};

}

template < typename ArithmeticType >
class arithmetic_traits {
public:

typedef ArithmeticType value_type;
typedef typename
DO_NOT_USE::signed_type< ArithmeticType >::the_type signed_type;
typedef typename
DO_NOT_USE::unsigned< ArithmeticType >::the_type unsigned_type;
};


Best

Kai-Uwe Bux

Gianni Mariani
Guest
 
Posts: n/a
#6: Nov 29 '05

re: typedef question


Kai-Uwe Bux wrote:[color=blue]
> Gianni Mariani wrote:[/color]
....[color=blue][color=green]
>>I would do this differently. First I would use a template class like:
>>
>>template <typename T>
>>struct Type
>>{
>> typedef T t_signed;
>> typedef unsigned T t_unsigned;[/color]
>
>
> This will not fly: unlike const or volatile, unsigned is not a qualifier. It
> cannot be stripped of or added by a template in a straight forward way. If
> you want a template to yield the (un)signed version of a type, you need to
> do a bunch of partial specializations like, for instance:
>[/color]

You are right.
Alex O.
Guest
 
Posts: n/a
#7: Dec 2 '05

re: typedef question


Thanks.
Using a template is a good idea.

Closed Thread


Similar C / C++ bytes