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.
c ++ templates specialization
tauran
source share