Specialization in a single C ++ template with several template parameters

Hallo!

I would like to specialize in only one of two types of templates. For example. template <typename A, typename B> class X must have a special implementation for one function X<float, sometype>::someFunc() .

Code example:

main.h:

 #include <iostream> template <typename F, typename I> class B { public: void someFunc() { std::cout << "normal" << std::endl; }; void someFuncNotSpecial() { std::cout << "normal" << std::endl; }; }; template <typename I> void B<float, I>::someFunc(); 

main.cpp:

 #include <iostream> #include "main.h" using namespace std; template <typename I> void B<float, I>::someFunc() { cout << "special" << endl; } int main(int argc, char *argv[]) { B<int, int> b1; b1.someFunc(); b1.someFuncNotSpecial(); B<float, int> b2; b2.someFunc(); b2.someFuncNotSpecial(); } 

Compilation error for class B Is it true that this is not possible in C ++ this way? What would be a better way?

[edit]

template <float, typename I> void B<float, I>::someFunc(); leads to main.h: 26: error: 'float is not a valid type for the template constant parameter

template <typename I> void B<float, I>::someFunc(); leads to main.h: 27: error: invalid use of incomplete type "class B

And I use gcc.

[edit]

I do not want to specialize the whole class, as there are other functions that do not have specialization.

+7
c ++ templates specialization
source share
3 answers

You must provide a partial specialization of the class B template:

 template <typename I> class B<float, I> { public: void someFunc(); }; template <typename I> void B<float, I>::someFunc() { ... } 

You can also just define someFunc inside the specialization.

However , if you only want to specialize a function, not a class. g.

 template <typename F, typename I> void someFunc(F f, I i) { someFuncImpl::act(f, i); } template <typename F, typename I> struct someFuncImpl { static void act(F f, I i) { ... } }; // Partial specialization template <typename I> struct someFuncImpl<float, I> { static void act(float f, I i) { ... } }; 

But you cannot specialize a function template without this trick.

+17
source share

Although you can fully specialize the member functions of a class template, you cannot specialize the member functions. - Andrey Alexandrescu

Partial class specialization is explained by other posters.

However, you can use overloading:

 template <class T, class U> T fun(U obj); // primary template template <class U> void Fun<void, U>(U obj); // illegal pertial // specialization template <class T> T fun (Window obj); // legal (overloading) 

If you want to delve into this, you can study this problem in detail in A. Alexandrescu's "Modern C ++ Design".

+5
source share

Solution 1. Move the entire implementation to a base class, such as B_Base. then specialize in float to override someFunc. as below

  template <typename F, typename I> class B : B_Base<F, I> { } template <typename I> class B<float, I> : B_Base<flat, I> { public: void someFunc() {....} }; 

Solution 2. use overload function, put float as input or boost :: is_same to send. unfortunately, your someFunc function has no parameter. so you need to change the interface.

0
source share

All Articles