-
April 25th, 2009, 07:03 PM
#1
[RESOLVED] Problems Using New Types
When I use typedef to create a new type, the compiler gives me errors if I try to tag modifying keywords on. For example, if I do this:
Code:
typedef char* cstring;
void function (const cstring string) {}
If I then try to call that function with a const char*, it gives me a compile error that there is no
Code:
void function (const char* string) // (With the const)
But the candidate is:
Code:
void function (char* string) // (Without the const)
Can someone help me here? What's going on?
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 25th, 2009, 08:01 PM
#2
Re: Problems Using New Types
Ehhh I despise this problem. I still don't agree with it haha.
-
April 25th, 2009, 08:12 PM
#3
Re: Problems Using New Types
Originally Posted by Mybowlcut
Ehhh I despise this problem. I still don't agree with it haha.
Thanks. That was very informative. And yes, it seems ridiculous. Who would design a language like that? If I write const, then just maybe I'd like it to be const
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 25th, 2009, 08:28 PM
#4
Re: Problems Using New Types
Originally Posted by Etherous
Thanks. That was very informative. And yes, it seems ridiculous. Who would design a language like that? If I write const, then just maybe I'd like it to be const
It is const, in exactly the manner you asked for it to be. You're trying to assume that typedefs are simply text substitutions - they aren't. And needless to say, trying to argue that something is stupid just because it doesn't work like you want it to work is, well, a bit stupid.
-
April 25th, 2009, 09:16 PM
#5
Re: Problems Using New Types
Originally Posted by Speedo
It is const, in exactly the manner you asked for it to be. You're trying to assume that typedefs are simply text substitutions - they aren't. And needless to say, trying to argue that something is stupid just because it doesn't work like you want it to work is, well, a bit stupid.
I want something const, so I put const in front of it. Why else would it be there? I don't care if it overwrites the constness of the type used. When someone explicitly says something, it should explicitly happen. So, yes, how it is setup is stupid. It has nothing to do with thinking it's a text substitution. It has everything to do with explicit, predictable semantic behavior
btw, solved this by making separate types for const (i.e. for cstring it was : typedef const char * const c_cstring_c; ) A bit sloppy in my oppinion, but it works
Last edited by Etherous; April 25th, 2009 at 09:45 PM.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 25th, 2009, 09:54 PM
#6
Re: Problems Using New Types
Originally Posted by Etherous
It has everything to do with explicit, predictable semantic behavior
The very nature of pointers dictates that they can't work exactly like everything else, because they have to deal with the const'ness of two objects - a pointer and the object it points to.
The behavior of typedefs, OTOH, is 100% constitent. This code
Code:
typedef T MyType;
const MyType foo;
will always yield a "const object of type T". What you're asking for would lead to extremely inconsistent behavior with typedefs. But then, you yourself said:
I don't care if it overwrites the constness of the type used.
-
April 25th, 2009, 10:03 PM
#7
Re: [RESOLVED] Problems Using New Types
The whole problem here is that you can't do something like this:
Code:
typedef T MyType;
const MyType foo;
The compiler will simply throw the 'const' out and simply use T. That's why I was having these problems in the first place
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 25th, 2009, 10:20 PM
#8
Re: [RESOLVED] Problems Using New Types
The compiler will simply throw the 'const' out and simply use T.
No, it does not.
Code:
typdef char* cstring;
const cstring foo;
foo is of the type "const cstring". The type of cstring is "pointer to char", so foo becomes then "const pointer to char". Without typedefs this would be written as "char* const".
For "const cstring" to translate into "pointer to const char" as you're wanting it to be, the compiler would have to silently modify the typedef made by you - which would most definetly be inconsistent and not a good thing.
Edit: Your problems seem to be with the semantics of pointers themselves. Like I said before, you have to deal with the const'ness of the pointer itself and the object it points to. It can be confusing, but it's kind of a necessary evil (like pointers themselves). Because of that, pointers come in 4 const flavors:
Code:
// pointer to T
T*
// pointer to const T
const T*
T const*
// const pointer to T
T* const
// const pointer to const T
const T* const
T const* const
Last edited by Speedo; April 25th, 2009 at 10:25 PM.
-
April 25th, 2009, 10:22 PM
#9
Re: [RESOLVED] Problems Using New Types
Okay. This makes more sense. I'd thought that the compiler was just ignoring the const. That's how the error messages were making it look. Thanks
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|