partial-specialization

c++ partial specialization: How can I specialize this template<class T1, class T2> to this template<class T1>?

我的未来我决定 提交于 2019-12-01 21:33:18
#include <iostream> using namespace std; template <class T1, class T2> class A { public: void taunt() { cout << "A"; } }; template <class T1> class A<T1, T1> { public: void taunt() { cout << "B"; } }; class B {}; class C {}; int main (int argc, char * const argv[]) { A<B> a; return 0; } How can I convert my two parameter template to a one parameter template? The above code will give a compiler error on 'A a;' for 'wrong number of template arguments'. Template specialization can't be used to reduce the number of template arguments, to do that you should use defaults for some of the arguments.

c++ pimpl idiom : Implementation depending on a template parameter

荒凉一梦 提交于 2019-12-01 15:10:50
In this question I unsuccessfully asked how to use different pimpl implementation depending on a template argument. Maybe this example ilustrates better what I am trying to do : #include <iostream> template< int N, typename T > struct B { B() : c( new C< N > ) {} template< int M > struct C; C< N > *c; }; template< int N, typename T > template< int M > struct B< N, T >::C { int a[M]; }; // version 1 that doesn't work template< int N, typename T > template< > struct B< N, T >::C< 0 > { int a; }; // version 2 that doesn't work template< typename T > template< int M > struct B< 0, T >::C { int a;

c++ pimpl idiom : Implementation depending on a template parameter

不羁的心 提交于 2019-12-01 13:24:45
问题 In this question I unsuccessfully asked how to use different pimpl implementation depending on a template argument. Maybe this example ilustrates better what I am trying to do : #include <iostream> template< int N, typename T > struct B { B() : c( new C< N > ) {} template< int M > struct C; C< N > *c; }; template< int N, typename T > template< int M > struct B< N, T >::C { int a[M]; }; // version 1 that doesn't work template< int N, typename T > template< > struct B< N, T >::C< 0 > { int a; }

Partial member function template specialisation and data member access

梦想的初衷 提交于 2019-12-01 06:21:30
I have a question regarding partial specialisation of templated member functions. Background: The goal is to compute descriptive statistics of large datasets which are too large to be hold in memory at once. Therefore I have accumulator classes for the variance and the covariance where I can push in the datasets piece by piece (either one value at a time or in larger chunks). A rather simplified version computing the arithmetic mean only is class Mean { private: std::size_t _size; double _mean; public: Mean() : _size(0), _mean(0) { } double mean() const { return _mean; } template <class T>

Partial member function template specialisation and data member access

邮差的信 提交于 2019-12-01 05:24:36
问题 I have a question regarding partial specialisation of templated member functions. Background: The goal is to compute descriptive statistics of large datasets which are too large to be hold in memory at once. Therefore I have accumulator classes for the variance and the covariance where I can push in the datasets piece by piece (either one value at a time or in larger chunks). A rather simplified version computing the arithmetic mean only is class Mean { private: std::size_t _size; double

Function template specialization with a template class [duplicate]

两盒软妹~` 提交于 2019-11-30 15:38:29
Possible Duplicate: partial specialization of function template I can't find anywhere a solution for my problem, because if I search with the keywords I come up with would give me solutions suited for different problems. I understand that this must been asked before, just can't find a solution. Suppose I have a function template: template<class any> print(any value); I can specialize it like this for let's say a int : template<> print<int>(int value) { std::cout << value; } But now the problem, I want it to work with a vector as well. Since the vector class is a template class it becomes

Partial specialization of double-templated method fails

╄→尐↘猪︶ㄣ 提交于 2019-11-30 14:19:43
There is the template class List. template <typename Point> class List { public: template <const unsigned short N> void load ( const char *file); ... }; template <typename Point> template <const unsigned short N> void List <Point>::load ( const char *file) } How to specialize method load for N=2? This code is not valid... template <typename Point> void List <Point> <2>::load ( const char *file) { } And this code also does not work. template <typename Point> void List <Point> ::load <2> ( const char *file ) { } Error 3 error C2768: 'List<Point>::load' : illegal use of explicit template

How to specialize only some members of a template class?

﹥>﹥吖頭↗ 提交于 2019-11-30 05:10:09
Code: template<class T> struct A { void f1() {}; void f2() {}; }; template<> struct A<int> { void f2() {}; }; int main() { A<int> data; data.f1(); data.f2(); }; ERROR: test.cpp: In function 'int main()': test.cpp:16: error: 'struct A<int>' has no member named 'f1' Basically, I only want to specialize one function and use the common definition for other functions. (In actual code, I have many functions which I don't want to specialize). How to do this? Thanks! Consider moving common parts to a base class: template <typename T> struct ABase { void f1(); }; template <typename T> struct A : ABase

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

爱⌒轻易说出口 提交于 2019-11-30 03:54:45
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 that object..."); } void deleteSome (T o) { printf ("deleting that object..."); } }; template <typename T> class foo <T *> { public: void deleteSome (T* o) { printf ("deleting that PTR to an object..."); } }; The user code foo<myclass> myclasses; foo<myclass*> myptrs; myptrs.addSome

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

a 夏天 提交于 2019-11-30 03:12:02
问题 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 class, then it's easy, I can just write: namespace std { namespace tr1 { template <typename Derived> struct hash<CRTPBase<Derived> > { size_t operator()