Specialization of the function of a member of a template class for only one type

I have a template class that has many functions, but is essentially a vector class. I want to add one function only to the bool type.

#include <vector> template <typename T> class reflected{ private: T*dev_; T*host_; ar_size size; reflected<T>& operator=(reflected<T>& rhs);//do not impliment. assign not allowed. reflected( reflected<T>& old); //do not impliment. Copy not allowed. public: reflected():size(0L),dev_(NULL),host_(NULL){} reflected(ar_size n):size(n),dev_(NULL),host_(NULL){init();} reflected(const T*x,ar_size n):size(n),dev_(NULL),host_(NULL){init();set(x);} ~reflected(); void init(); void init(ar_size n); void init(const T*x,ar_size n); void set(const T * x); void setat(ar_index i, T x); const T getat(ar_size i); const T * devPtr(); const T operator [](const ar_index i); ar_size length(){return size;} }; 

I want to add the vector<ar_index> reflected<bool>::which() function to the special case of the reflected class, which is the only case where this makes sense. What is the best way to do this. the compiler does not seem to like adding which () to reflect and define only for bool.

+4
source share
4 answers

You can define it in a class template like this

 template <typename T> struct id { typedef T type; }; template <typename T> class reflected{ private: /* ... */ vector<ar_index> which(id<bool>) { /* ... */ } public: /* ... */ vector<ar_index> which() { return which(id<T>()); } }; 

This gives a compile-time error if you call which on a reflected<T> , for which T you did not give the correct definition.

+12
source

If you want to add only one question, you can combine inheritance with specialization:

 template <typename T> class reflected_base { // your current 'reflected' contents go here }; template <typename T> class reflected : public reflected_base { }; template <> class reflected<bool> : public reflected_base { vector<ar_index> which(); }; 

The disadvantage of this approach is that for each specialization you need to redefine certain operations (destructors, copy constructors, etc.). Another variant:

 template <typename T> class specialized_reflected { }; template <> class specialized_reflected<bool> { public: vector<ar_index> which(); }; template <typename T> class reflected : public specialized_reflected<T> { // your current 'reflected' contents go here }; 

Although, there are potential problems with the dependent name. The third option (and probably the one I would choose) would be to use a non-member function:

 vector<ar_index> which(reflected<bool>&); 
+4
source

It is impossible to do the way you want. But you can achieve a similar result without defining reflected() for any class except the specialized one. Then you get a ruler error if you try to use it in an unsupported class.

 #include <string> #include <sstream> using namespace std; template<typename A> class Gizmo { public: Gizmo(){}; int which(); }; template<> int Gizmo<bool>::which() { return 42; } int main() { Gizmo<bool> gb; gb.which(); Gizmo<int> gi; gi.which(); // LINKER ERROR for Gizmo<int>which() return 0; } 
+1
source

You can add vector<ar_index> reflected<bool>::which() to reflected<bool> (and only to it, and not to the general template). If you get an error message, you may not be properly specializing the template ...

0
source

All Articles