Thread: Double template specializations

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Double template specializations

    Hi, it has been a while since I created a thread in this forum.

    I'm having trouble with specializing a templated function inside a templated class. If the class is a regular class it works fine, but I need the class to be templated too.

    Here's a class with everything unimportant stripped off:
    Code:
        template<class C>
        class Logger
        {
            public:
    
                template<typename T>
                    void log(T str);
        };
        
        
    
        //Main definition (works)
        template<class C> 
        template<typename T>
        void Logger<C>::log(T str)
        {
            
        }
        
        //Specialization for ints
        template<class C> 
        template<>
        void Logger<C>::log<int>(int str)
        {
            
        }
    How do I accomplish that last specialization? As of now, it doesn't work.

    Here's how it would look if the class wasn't templated and this works:
    Code:
        class Logger
        {
            public:
    
                template<typename T>
                    void log(T str);
        };
        
        template<typename T>
        void Logger::log(T str)
        {
            
        }
    
        template<>
        void Logger::log<int>(int str)
        {
            
        }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What's the disadvantage of just overloading the function for integers in the class defintion? Is this what you wanted?
    Code:
    #include <iostream>
    
    template <class C>
    class foo {
       public:
          template <typename T>
          void bar(T var);
          
          void bar(int var);
    };
    
    template <class C>
    template <typename T>
    void foo<C>::bar(T var) {
       std::cout << "This is the templated function." << std::endl;
    }
    
    template <class C>
    void foo<C>::bar(int var) {
       std::cout << "This is the overloaded function for ints." << std::endl;
    }
    
    int main() {
       foo<int> obj1;
       
       obj1.bar(3.14);
       obj1.bar(3);
       obj1.bar("Foo");
       
       return 0;
    }
    
    /* Output:
    This is the templated function.
    This is the overloaded function for ints.
    This is the templated function. */
    Sent from my iPad®

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm having trouble with specializing a templated function inside a templated class.
    You have to specialize the class as well. C++ doesn't yet allow what you're trying to do.
    My best code is written with the delete key.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    How's Time travelling going for you?

    Just out of curiosity, I only have a small knowledge of templating, why do you have 'template<>' before your void Logger::log<int> thing.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by twomers
    How's Time travelling going for you?

    Just out of curiosity, I only have a small knowledge of templating, why do you have 'template<>' before your void Logger::log<int> thing.
    Because that's required syntax for template specialization. As, I was saying though, I don't see the need for template specialization here. I might be misunderstanding though. I feel like overloading the function is just as easy for the special case. Does the compile still generate a int version of the templated function when you just overload as I did?
    Last edited by SlyMaelstrom; 08-23-2006 at 07:13 AM.
    Sent from my iPad®

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    the code is correct - the problem is that most compilers don't support that type of specialization within template classes (yet).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    What's the disadvantage of just overloading the function for integers in the class defintion? Is this what you wanted?
    [...]
    As, I was saying though, I don't see the need for template specialization here.
    That is less elegant and my example is very simlplified, but I still think I'll have to do something like that.
    You have to specialize the class as well. C++ doesn't yet allow what you're trying to do.
    I suspected something like that after trying seveal methods in two compliers.

    Thanks everyone.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Sang-drax
    That is less elegant and my example is very simlplified, but I still think I'll have to do something like that.
    Elegant shmelegant... if it's the same when it becomes assembly, then who cares. Called the same, operates the same, compiles the same... sure it may seem more proper to use a specialized template, but really, in the end does it mean a thing?
    Sent from my iPad®

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if it's the same when it becomes assembly, then who cares
    The people who maintain it, obviously.

    >but really, in the end does it mean a thing?
    Yes, it means a great deal.
    My best code is written with the delete key.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Prelude
    Yes, it means a great deal.
    Care to elaborate?
    Sent from my iPad®

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by SlyMaelstrom
    Care to elaborate?
    If syntax was irrelevant, we could as well be programming assembly.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Care to elaborate?
    When code is elegant, it's transparent, efficient, and surprisingly simple. An inelegant solution is often a poor solution that's difficult to maintain. Claiming that "it's okay as long as the machine code is the same" is a cop out that breeds unmaintainable code.
    My best code is written with the delete key.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Sang-drax
    If syntax was irrelevant, we could as well be programming assembly.
    Ah, but it's not irrelevant, and that's my point. Cause you're sitting on two options here. Overloading the function as per my suggestion which you said you would likely have to do or you have to sit on your program while you wait for those "yets" that cropped up in this post to disappear. Even if some compilers do take that syntax and you were to hunt one of those compilers down, you're now creating code that's not (yet) as portable as the other option. Now your piece of code that was once so elegant doesn't quite look so elegant, anymore.

    I would also suspect that any programmer maintaining the code would likely expect the portable solution to the answer and have no problem with it.

    Regardless, I'm still not even sure if what I originally posted is the best alternative. I had asked that then, a bit later in the topic, and still have yet to get an answer.

    Quote Originally Posted by Prelude
    When code is elegant, it's transparent, efficient, and surprisingly simple. An inelegant solution is often a poor solution that's difficult to maintain. Claiming that "it's okay as long as the machine code is the same" is a cop out that breeds unmaintainable code.
    I don't disagree with this, at all, but we're not talking about a nice, clean, portable solution vs. an obfuscated mess. We're talking about some nice, but not yet standard (at least as far as compilers are concerned) code vs. what I understand to be the best alternative. Again, I'd love for someone to correct me on that, though.
    Last edited by SlyMaelstrom; 08-23-2006 at 09:13 AM.
    Sent from my iPad®

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's a popular and highly regarded C++ book, I forgot its name, which always recommends overloading over specialization. I forgot why.
    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. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM