0

Hi,
I am playing with metaprogramming. I have this problem:

template<bool f>
class A
{
    public:
        void P()
        {
             cout << "f = true" << endl;
        }

        void Q()
        {
             cout << "Q" << endl;
        }
};

template<>
class A<false>
{
    public:
        void P()
        {
             cout << "f = false" << endl;
        }

        void Q()
        {
             cout << "Q" << endl;
        }
};

because The Q function is not changed, I would prefer not to write it again in A<false>. Is there any way to do that?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by twomers
0

Haven't done template programming in a while, but as far as I remember you only need to specialise a function rather than the entire class. I think this should work, if not it should give you a good idea how to do what you want...

template <typename ty>
class thing {
public:
  void one();

};

template<typename ty>
void thing<ty>::one() {
  std::cout<< "thing<ty>::one() called...\n";
}

template<>
void thing<bool>::one() {
  std::cout<< "thing<bool>::one() called...\n";
}

int main( void ) {
  thing<bool> boolThing;
  thing<int> intThing;

  boolThing.one();
  intThing.one();

  return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.