partial-specialization

"invalid use of incomplete type" error with partial template specialization

The following code: template <typename S, typename T> struct foo { void bar(); }; template <typename T> void foo <int, T>::bar() { } gives me the error invalid use of incomplete type 'struct foo<int, T>' declaration of 'struct foo<int, T>' (I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the...

What are some other languages that support "partial specialization"?

Partial template specialization is one of the most important concepts for generic programming in C++. For example: to implement a generic swap function: template <typename T> void swap(T &x, T &y) { const T tmp = x; y = x; x = tmp; } To specialize it for a vector to support O(1) swap: template <typename T, class Alloc> void swa...

partial template specialization for dynamic dispatch

...

How to do template specialization in C#

How would you do specialization in C#? I'll pose a problem. You have a template type, you have no idea what it is. But you do know if its derived from XYZ you want to call .alternativeFunc(). A great way is to call a specialized function or class and have normalCall return .normalFunc() while have the other specialization on any derived ...

How to partially specialize a class template for all derived types?

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is implemented in the CRTP base class. If I only want to partially specialize for a the CRTP base...

pointers as template parameters?

I have a container class, we'll call it template <class T> CVector { ... } I want to do something different with this class when T is a pointer type, e.g. something along the lines of: template <class T*> CVector< SomeWrapperClass<T> >; where SomeWrapperClass is expecting the type of the pointed to thing as its parameter. Unfort...

Template Partial Specialization - any real-world example?

I am pondering about partial specialization. While I understand the idea, I haven't seen any real-world usage of this technique. Full specialization is used in many places in STL so I don't have a problem with that. Could you educate me about a real-world example where partial specialization is used? If the example is in STL that would b...

Templated parameter for a template specialisation?

Hi I've got a static member of a templated class that I want defined for a sub group of classes that are templated ie: template <typename T> class FooT { private: static int ms_id; }; template <typename T> class Foo {}; template<> template<typename T> int FooT< template Foo<T> >::ms_id = 10; Sadly this throws the following error ...

C++ template specialization with <int&> not picking up an int

Hi, I have the following code: template <typename T> LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0); } template <> LuaCall& operator>><int&>(int& val) { mResults.push_back(std::make_pair(LUA_RESULT_INTEGER, (void *)&val)); return *this; } template <> LuaCall& operator>><float&>(float& val) { mResults.push_back(std::make_pa...

C++ template partial specialization - specializing one member function only

Hi everybody, Bumped into another templates problem: The problem: I want to partially specialize a container-class (foo) for the case that the objects are pointers, and i want to specialize only the delete-method. Should look like this: The lib code template <typename T> class foo { public: void addSome (T o) { printf ("adding...

specializing functions on stl style container types

If i have a type T, what is a useful way to inspect it at compile time to see whether its an STL-style container (for an arbitrary value type) or not? (Assumption: pointers, reference, etc. already stripped) Starting code: template<class T> // (1) void f(T&) {} template<class T> // (2) void f(std::vector<T>&) {} void test() { ...

Can I use partial template specialization for a (non-member) function?

I'm trying to use partial template specialization on a (non-member) function, and I'm tripping up on the syntax. I've searched StackOverflow for other partial template specialization questions, but those deal with partial specialization of a class or member function template. For a starting point, I have: struct RGBA { RGBA(uint8 ...

specialize a member template without specializing its parent

I have a class template nested inside another template. Partially specializing it is easy: I just declare another template< … > block inside its parent. However, I need another partial specialization that happens to specify all its local template arguments. This makes it into an explicit specialization. Explicit specializations, for wha...

Partial template specialization of free functions - best practices

As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++: template <class T, int N> T mul(const T& x) { return x * N; } template <class T> T mul<T, 0>(const T& x) { return T(0); } // error: function template partial specialization ‘mul<T, 0>’ is not...

Get the signed/unsigned variant of an integer template parameter without explicit traits

I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T, and the other as the unsigned variant of type T -- i.e. if T == int, then T_Unsigned == unsigned int. My first instinct was to do this: template <typename T> class Range { typedef unsign...

how to templatize partial template specializations?

I'm not even sure what title to give this question; hopefully the code will demonstrate what I'm trying to do: #include <string> #include <list> using namespace std; template<typename A> class Alpha { public: A m_alpha_a; }; template<typename B> class Bravo { public: B m_bravo_b; }; template<> class Alpha<string> { public: string m_al...

Partial specialization with reference template parameter fails to compile in VS2005

I have code that boils down to the following: template <typename T> struct Foo {}; template <typename T, const Foo<T>& I> struct FooBar {}; //////// template <typename T> struct Baz {}; template <typename T, const Foo<T>& I> struct Baz< FooBar<T,I> > { static void func(FooBar<T,I>& value); }; //////// struct MyStruct { static con...

Partial template specialization: matching on properties of specialized template parameter

template <typename X, typename Y> class A { // Use Y::Q, a useful property, not used for specialization. }; enum Property {P1,P2}; template <Property P> class B {}; class C {}; Is there any way to define a partial specialization of A such that A<C, B<P1> > would be A's normal template, but A<C, B<P2> > would be the specialization? ...

C++: Partial template specialization

Hi, I'm not getting the partial template specialization. My class looks like this: template<typename tVector, int A> class DaubechiesWavelet : public AbstractWavelet<tVector> { // line 14 public: static inline const tVector waveletCoeff() { tVector result( 2*A ); tVector sc = scalingCoeff(); for(int i = 0; i < 2*A; ++i)...

CPP templated member function specialization

Hi, I'm trying to specialize the member function moment() only (not the hole class) like this: template<class Derived, class T> class AbstractWavelet { public: [...] template<bool useCache> const typename T::scalar moment(const int i, const int j) const { return abstractWaveletSpecialization<Derived, T, useCache>::moment(sta...