1

I have two classes class A and class B both of them are template classes for a member function in A I want it to act in a special way when the type of A is B and in a normal way for any other types I don't know how to do this ?

template <class B>
class B
{
private:
  T m;
public:
  ...... any member functions
}

template <class T>
class A
{
private:
  T var;
public:
  void doSomething();
};
template <class T>
void A<T>::doSomething(){...........//implementation}
template <class T> 
void A<B<T>>::doSomething(){................//different implementation}
  • See the accepted answer of http://stackoverflow.com/questions/165101/invalid-use-of-incomplete-type-error-with-partial-template-specialization. It answers your question also. – Lingxi Apr 25 '15 at 17:17
  • Didn't you already ask this question ([How to specialize template member function for another template class?](http://stackoverflow.com/questions/29837533/how-to-specialize-template-member-function-for-another-template-class/29837877#29837877))? – James Adkison Apr 25 '15 at 20:40
  • Yes, but It was misunderstood so I tried to reshape it in a different way also neither of the answers could work @JamesAdkison – Fakhreldin Mohamed Apr 25 '15 at 20:49
  • I don't see how this question is any different but regardless the answers here are the same as you already received and in the post @Lingxi mentions. – James Adkison Apr 25 '15 at 20:59
  • The two questions have different shapes they are for the same problem but it is not solved at first I thought what Lingxi said was the solution then after I tried using this it failed your solution is the same but you didn't explain it well but regardless both didn't solve my problem @JamesAdkison – Fakhreldin Mohamed Apr 25 '15 at 22:54

2 Answers2

0

You can specialize A this way:

template <class T>
class A<B<T>> {
   // ...
};

This is an instance of partial template specialization.

If you refuse to specialize the entire class, you can defer the work from A<T>::doSomething() to a function doSomethingForA<T>(A &) that would be partially specialized, and that would possibly be friend of A<T>.

Ekleog
  • 1,054
  • 7
  • 19
0

Hope this solves your problem:

#include <iostream>

template <typename T>
struct B {};

template <typename T> struct A;

template <typename T>
void doSomething(T&) { std::cout << "General\n"; }
template <typename T>
void doSomething(A<B<T>>&) { std::cout << "Special\n"; }

template <typename T>
struct A {
  void doSomething() {
    ::doSomething(*this);
  }  
};

int main()
{
    A<int> general;
    A<B<int>> special;
    general.doSomething();
    special.doSomething();
}
Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • It worked (Y) but can it take any other parameters ? or just T& and A>& ? @Lingxi – Fakhreldin Mohamed Apr 25 '15 at 17:57
  • I have a problem I wanted to deal with class data members as if those functions are member functions how can I do this? @Lingxi – Fakhreldin Mohamed Apr 25 '15 at 19:08
  • @FakhreldinMohamed You can do it in a few ways. For example: pass the state to be modified to the function performing the modification, pass an instance of the class and make the function performing the changes a friend, etc... – James Adkison Apr 25 '15 at 21:04
  • both of solutions didn't work the problem is I need the function to be member function not to call setters or getters or even be a friend. @JamesAdkison – Fakhreldin Mohamed Apr 25 '15 at 21:41
  • These are the solutions you have available and if you can't use them you can **_specializing the entire_** class. Without more specifics we can't say why you can't get the answers to work but or show you how to get it working. – James Adkison Apr 25 '15 at 21:48