* Stupid typedef question / g++
@ 2003-01-24 15:15 SA
2003-01-24 16:55 ` Ben Davis
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: SA @ 2003-01-24 15:15 UTC (permalink / raw)
To: gcc-help
Should this work?
// start
#include <stdio.h>
typedef int TYPE1;
typedef int TYPE2;
void test(TYPE1);
void test(TYPE2);
void test(TYPE1 in){
printf("Called via type1 %d\n",(int)in);
}
void test(TYPE2 in){
printf("Called vis type2 %d\n",(int)in);
}
main(){
test((TYPE1)10);
test((TYPE2)20);
}
// finish
Now I expected the typedefs and casts to enable correct function selection
but what I got was
g++ test.cc
test.cc: In function `void test (int)':
test.cc:12: redefinition of `void test (int)'
test.cc:8: `void test (int)' previously defined here
Which is what I would expect if I had used
#define TYPE1 int
#define TYPE2 int
instead?
Comments, suggestions? I wanted to use the typedefing to control the function
calling for elegance in my program.
Thanks SA
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stupid typedef question / g++
2003-01-24 15:15 Stupid typedef question / g++ SA
@ 2003-01-24 16:55 ` Ben Davis
2003-01-24 16:56 ` Sebastian Huber
2003-01-24 17:08 ` John Love-Jensen
2 siblings, 0 replies; 4+ messages in thread
From: Ben Davis @ 2003-01-24 16:55 UTC (permalink / raw)
To: SA, gcc-help
(Sorry if you get too many copies of this, SA. I got the address wrong for
the mailing list, and I didn't know if you'd got your copy :)
On Friday 24 January 2003 3:17 pm, you wrote:
> Should this work?
>
> // start
> #include <stdio.h>
> typedef int TYPE1;
> typedef int TYPE2;
typedef simply gives you a name by which to refer to a (possibly more
complex) type. I'm afraid it doesn't offer any segregation between the types.
You could remove the casts and your code would still be completely valid.
I too would be interested to know of a way of doing this...
Ben
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stupid typedef question / g++
2003-01-24 15:15 Stupid typedef question / g++ SA
2003-01-24 16:55 ` Ben Davis
@ 2003-01-24 16:56 ` Sebastian Huber
2003-01-24 17:08 ` John Love-Jensen
2 siblings, 0 replies; 4+ messages in thread
From: Sebastian Huber @ 2003-01-24 16:56 UTC (permalink / raw)
To: gcc-help
Hi,
typedefs provide only type aliasses, they do not define new types. User
defined types can be created via class (or struct) and in a more limited way
with enums.
On Friday 24 January 2003 16:17, SA wrote:
> Should this work?
>
> // start
> #include <stdio.h>
> typedef int TYPE1;
> typedef int TYPE2;
>
> void test(TYPE1);
> void test(TYPE2);
>
> void test(TYPE1 in){
> printf("Called via type1 %d\n",(int)in);
> }
>
> void test(TYPE2 in){
> printf("Called vis type2 %d\n",(int)in);
> }
>
> main(){
> test((TYPE1)10);
> test((TYPE2)20);
> }
> // finish
>
> Now I expected the typedefs and casts to enable correct function selection
> but what I got was
>
> g++ test.cc
> test.cc: In function `void test (int)':
> test.cc:12: redefinition of `void test (int)'
> test.cc:8: `void test (int)' previously defined here
>
> Which is what I would expect if I had used
> #define TYPE1 int
> #define TYPE2 int
> instead?
>
> Comments, suggestions? I wanted to use the typedefing to control the
> function calling for elegance in my program.
>
> Thanks SA
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stupid typedef question / g++
2003-01-24 15:15 Stupid typedef question / g++ SA
2003-01-24 16:55 ` Ben Davis
2003-01-24 16:56 ` Sebastian Huber
@ 2003-01-24 17:08 ` John Love-Jensen
2 siblings, 0 replies; 4+ messages in thread
From: John Love-Jensen @ 2003-01-24 17:08 UTC (permalink / raw)
To: SA, gcc-help
Hi SA,
No, that will not work. C++ does not have an "explicit typedef", rather it
has an aliasing typedef.
You can do something like this:
struct TYPE1 { int m; TYPE1(int in) : m(in) {} };
struct TYPE2 { int m; TYPE2(int in) : m(in) {} };
void test(TYPE1);
void test(TYPE2);
...et cetera. You can add more convenience routines to the two types, to
make them spoof "being an int" as much as you require.
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-01-24 17:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-24 15:15 Stupid typedef question / g++ SA
2003-01-24 16:55 ` Ben Davis
2003-01-24 16:56 ` Sebastian Huber
2003-01-24 17:08 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).