Thread: template code generation

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    template code generation

    Suppose you made a really complicated template class, and you use it with many kinds of arg types. For example, set<Blah*>, set<Bloo*>, set<Moo*>, set<Mooya*> etc.

    But all the arg types are essentially pointer types.

    When compiled, will each of these instantiations have its own code template (aka get really bloated), or will each of these templates share a same code when compiled (set<void*>)?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Depends on the implementation. STLport is the only one that I know that enables this kind of optimizations, and I don't think the standard mandates it. Besides, there are limitations on the compilers that support it and on what you can accomplish with it. From STLport's user_config.h:

    To reduce the famous code bloat trouble due to the use of templates STLport grant
    a specialization of some containers for pointer types. So all instanciations
    of those containers with a pointer type will use the same implementation based on
    a container of void*. This feature has shown very good result on object files size
    but after link phase and optimization you will only experiment benefit if you use
    many container with pointer types.
    There are however a number of limitation to use this option:
    - with compilers not supporting partial template specialization feature, you won't
    be able to access some nested container types like iterator as long as the
    definition of the type used to instanciate the container will be incomplete.
    - you won't be able to use complex Standard allocator implementations which are
    allocators having pointer nested type not being a real C pointer.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Hmm.. I see. Any idea on whether g++ supports this? I just went to IRC and some guy started saying that: pointers can be of different sizes. I'm hoping this can be known during compile time?

  4. #4
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Pointers are always the same size on the same machine, even across different types. The size of a char * pointer will always be the same size as a std::map<std::string, std::vector<std::string> > * pointer.

    After all, a pointer is simply a variable that stores a memory address as a value. You can, therefore, get the size of a pointer with sizeof(). For example:

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[]) {
        std::cout << "On this system, the size of a pointer is: " << sizeof(char *) << std::endl;
        return 0;
    }
    Does that help at all?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by underthesun View Post
    Any idea on whether g++ supports this?
    No idea, to be honest. I seldom use libstdc++

    I just went to IRC and some guy started saying that: pointers can be of different sizes
    Yes, this is technically true. However, platforms where this is the case are extremely rare, and furthermore, all pointers can be cast to and from void* (Function and method pointers nonwithstanding). So as long as the underlying container holds the pointer types as void*, there should be no problem.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    Yeah, I thought that was a bit weird how he said it like that. That guy also said that void* is required to be able to contain the value of a pointer in any object.. anyhow I guess I'll start taking things with a grain of salt now.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    I will quote (from here):

    558 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.

    559 Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements.

    560 All pointers to structure types shall have the same representation and alignment requirements as each other.

    561 All pointers to union types shall have the same representation and alignment requirements as each other.
    Interesting . . .

    I can say that I've never run into a platform where pointer sizes are different, though . . . Well, you learn something every day.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    whoops, that reply was late. Anyhow, yeah, you do learn something new everyday would be a nice trick question hahaha

  9. #9
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by strange View Post
    I can say that I've never run into a platform where pointer sizes are different, though
    Me neither, but it's one of many technicalities present in the C/C++ standard. It's like the definition of a "byte"; neither language says that it HAS to be 8 bits... yet, platforms in which it isn't are a rare corner case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error with Fibonacci code generation
    By xeon1989 in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2009, 08:58 AM
  2. X server slaps some template code with a "fatal IO error"
    By Yarin in forum Linux Programming
    Replies: 8
    Last Post: 08-24-2009, 08:47 AM
  3. Reference Counting
    By Dae in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2009, 07:34 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM