CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    [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

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Problems Using New Types

    Ehhh I despise this problem. I still don't agree with it haha.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Problems Using New Types

    Quote Originally Posted by Mybowlcut View Post
    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

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Problems Using New Types

    Quote Originally Posted by Etherous View Post
    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.

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Problems Using New Types

    Quote Originally Posted by Speedo View Post
    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

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Problems Using New Types

    Quote Originally Posted by Etherous View Post
    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.

  7. #7
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    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

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    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.

  9. #9
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    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
  •  





Click Here to Expand Forum to Full Width

Featured