Thread: There has got to be a better way

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    There has got to be a better way

    It's a solution I came up with to a template specialization problem which is really crappy. And if there are any other, better, cleaner ways to do it, please enlighten me.

    I have a templated node class with a lot of member variables and functions. With the exception of ONE member function, everything else is the same. For this one exceptional function, the behavior is different depending on what the template type is. So here's my solution:

    Code:
    class A;
    class B;
    
    template <typename T>
    class node {
      void recursive_function(node<A>* nt) {
        do_foo;
        recursive_function(nt);
      }
    
      void recursive_function(node<B>* nt) {
        do_bar;
        recursive_function(nt);
      }
    }
    
    //main body of program
      node<A>* na = new node<A>;
      node<B>* nb = new node<B>;
    
      na->recursive_function(na);
      nb->recursive_function(nb);
    One solution would be to change it into an inheritance relationship, but I'd have to change many other places in my program to conditional statements. So I'd like to avoid doing that.

    Is there a better way to do what I'm doing?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure that the RIGHT way to do that would be, as you suggest, to have a base-class with a virtual function that you override in A and/or B to do do_foo and do_bar respectively.

    Or at least, let each node have a member function that does either do_foo or do_bar.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Sorry, I didn't give full details. The template node itself is subsumed under several other classes in different ways. Like
    Code:
    template<typename T>
    class OneClass {
    node<T> one_node;
    };
    
    template<typename T>
    class AnotherClass {
    node<T> another_node;
    };
    And it's actually these things that are being called by the main program. So if I modify "node" in a fundamental way like that, it'll take me a while to untangle all the other dependent modules.

    Anyway, thanks. Judging by your comment, I'm guessing there's no clean way of doing this with purely templatic methdods.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why, I'm pretty sure you can specialize member functions of a template class.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Could you tell me how? I've looked at several articles on template specialization and it doesn't seem as if specialization of template member functions is possible. I've tried some variations myself and the compiler tells me no dice.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream>
    
    template<typename T> class foo
    {
    public:
    	void foo2();
    };
    
    template<typename T> void foo<T>::foo2()
    {
    	 std::cout << "Hi, I'm foo!\n";
    }
    
    template<> void foo<int>::foo2()
    {
    	std::cout << "Hi, I'm a retarded foo!\n";
    }
    
    int main()
    {
    	foo<double> d;
    	foo<int> i;
    	d.foo2();
    	i.foo2();
    }
    Output:
    Hi, I'm foo!
    Hi, I'm a retarded foo!
    Press any key to continue . . .
    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.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Oh wow. That worked.

    Thanks Elysia. You're a programming master! How the hell do you know every thing?

Popular pages Recent additions subscribe to a feed