Thread: Questions on typedef

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Questions on typedef

    I notice you more experienced programmers use typedef a lot. I have some questions:

    1. When should you use typedef? When writing:
    Code:
    std::list<myType>::iterator
    100 times can be easier expressed as:
    Code:
    typedef std::list<myType>::iterator list_itr;
    list_itr it = a_list.begin();
    2. I notice often typedefs are written at global scope. Do typedefs follow normal scoping rules? Should they typically be written at global scope?

    EDIT:

    i guess that's actually:
    Code:
    std::list<myType>::std::iterator
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) You just answered your question. When the type becomes unwieldy, unreadable, or simply overly long for the number of times you'll use it, give it an alias.

    2) They follow scoping rules. They should be written in the scope where they're useful.
    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

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by dudeomanodude View Post
    Code:
    std::list<myType>::std::iterator
    I think that's extra.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    How do you typedef the following:

    Code:
    template <typename T> return_type my_template_class
    ???

    Do you include up to return_type or up to my_template_class?

    EDIT: I don't have a compiler right now, so I can't play around with it myself.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you talking about template typedefs? That language feature will only be available in C++0x.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by laserlight View Post
    Are you talking about template typedefs? That language feature will only be available in C++0x.
    I'm not sure...

    I mean if I have an implementation like this:
    Code:
    template <typename T> void template_container::do_something(){
    
        std::cout << "Hello there!\n";
    }
    can I shorten the whole template <typename T> void template_container line?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This doesn't look entirely valid, but you could surely shorten it by defining do_something within template_container definition
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't shorten an out-of-line definition, really.
    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

  9. #9
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Hmmm.

    I want to do something like this:

    Code:
    typedef template <typename T> void template_container void_t;
    
    void_t::do_something(){
    
        // ...
    
    }
    that's what I'm trying to get at.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think it's possible with typedefs. However, defines can still do the trick.
    (This is, of course, assuming template_container is a template, in which case in should look more like
    typedef template <typename T> void template_container<T> void_t;
    anyway.)
    Last edited by Elysia; 03-05-2008 at 12:45 PM.
    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.

  11. #11
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Code:
    #include <iostream>
    
    template <typename T> 
    class my_temp{
    
    	public:
    
    		void do_something();
    
    };
    
    typedef template <typename T> void my_temp void_t;
    void_t::do_something(){
    
        std::cout << "Hello there!\n";
    }
    
    int main(){
    
    	my_temp<int> example;
    	
    	example.do_something();
    	
    	return 0;
    }
    yea, it didn't like the typedef line.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unspecialized templates aren't allowed in C++. Unspecialized templates are where you specify the name of something that is a template, but do not specify a type. Example:
    Code:
    typedef my_temp mytypedef; // Illegal
    typedef my_temp<int> mytypedef; // Legal
    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.

  13. #13
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Unspecialized templates aren't allowed in C++. Unspecialized templates are where you specify the name of something that is a template, but do not specify a type.
    So basically when I write a template I can't abbreviate my template stuff (with typedefs), but when I use a template, it's fair game?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dudeomanodude View Post
    So basically when I write a template I can't abbreviate my template stuff (with typedefs), but when I use a template, it's fair game?
    No, you sure can do it, but you must specify the type. You can't use a specialized type such as T and you can't omit it:

    Code:
    typedef template<typename T> my_temp<T> mytd; // Illegal
    typedef my_temp mytd; // Illegal
    typedef my_temp<int> mytd; // Legal
    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.

  15. #15
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Okay, so the real litmus test is that the template argument itself must be a defined type?

    I want to get that right for my benefit and anyone else's.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

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