Thread: Questions on typedef

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think the real reason is that it simply won't allow for "template<typename T>" in a typedef, so you really can't use T as a type.
    Otherwise it's right that you must specify a type. You can't pass a templated type without specifying a type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Elysia View Post
    I think the real reason is that it simply won't allow for "template<typename T>" in a typedef, so you really can't use T as a type.
    Otherwise it's right that you must specify a type. You can't pass a templated type without specifying a type.
    Okay I think you've got to the heart of it->Why allow a typedef for a type that doesn't even exist.

    There's probably a better explanation than that out there, but perhaps that's the gist of it.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know why they did it, I only know it simply doesn't work.
    And what the standard says, goes. That's life.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A typedef is an alias for a type. You can only create aliases for actual types. A template name is not a type (except within the context of its definition, but that's a special case that would complicate things now). "template <typename T> foo" is not a type. "foo<T>" is a type, if T is a type or a type parameter, therefore you can give it an alias.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    Okay I think you've got to the heart of it->Why allow a typedef for a type that doesn't even exist.
    Because it would make life so much nicer. Suppose I want a quick way to refer to the iterator type of ANY kind of std::vector<T>. What I want to do is this:

    Code:
    template <typename T>
    typedef std::vector<T>::iterator vec_iterator;
    Then, I could use it like this:

    Code:
    std::vector<int> vec;
    for(vec_iterator<int> i = vec.begin(), end = vec.end(); i != end; ++i)
    {
        ...
    }
    vec_iterator, being a "template typedef," does not name a type. But vec_iterator<int> does. Unfortunately, this construct is not supported yet. In practice, you do this:

    Code:
    template <typename T>
    struct vec_iterator
    {
        typedef typename std::vector<T>::iterator type;
    };
    And then instead of referring to vec_iterator<int>, you refer to vec_iterator<int>::type. Does this suck? Yes. You have to declare a struct whose only purpose is to contain this typedef. But it's what we have for the moment.

    Bottom line: you can achieve the same things as if we had template typedefs, it's just ugly.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yay for C++0x, then.
    Code:
    template <typename T>
    using vec_iterator = std::vector<T>::iterator;
    Or something like that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    Yay for C++0x, then.
    Code:
    template <typename T>
    using vec_iterator = std::vector<T>::iterator;
    Or something like that.
    Why the gratuitous divergence from the syntax one would expect? That looks awful.

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There were some sound reasons not to use typedef, but I can't remember them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM