partial-specialization

What is wrong with partial template specialization?

你说的曾经没有我的故事 提交于 2019-12-05 20:37:46
I am writing a templated class with one type paramenter and one boolean, here is the code: template<class T, bool p = true> class A { private: T* ptr; public: A(); }; template<class T> A<T,true>::A() { ptr = 0xbaadf00d; } int main() { A<int> obj; A<int, false> o; return(0); } And I am getting these compilation errors: Error 1 error C3860: template argument list following class template name must list parameters in the order used in template parameter list tst.cpp 15 Error 2 error C2976: 'A<T,p>' : too few template arguments tst.cpp 15 What am I doing wrong? Or is it for some reason forbidden

C++ specialize template class function without duplicating code

二次信任 提交于 2019-12-05 13:54:18
I want to write 5 different classes, each of which has many member functions which are exactly the same, except of one, which is special for each class. Can I write this avoiding code duplication? Regards, Aleksejs Below is a very shortened version of my code, which throws the error: template_test.cpp:15:35: error: invalid use of incomplete type ‘class impl_prototype<cl, 1> #include <iostream> using namespace std; template <int cl, int dim> class impl_prototype { public: impl_prototype() {} int f(int x) { return cl + 2 * g(x); } int g(int x) { return cl + 1 * x;} }; template <int cl> int impl

Template Partial Specialization - any real-world example?

老子叫甜甜 提交于 2019-12-05 13:49:50
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 be superior! C++0x comes with unique_ptr which is a replacement for auto_ptr which is going to be deprecated. If you use unique_ptr with an array type, it uses delete[] to free it, and to provide operator[] etc. If you use it with a non-array type, it uses

why SFINAE (enable_if) works from inside class definition but not from outside

爷,独闯天下 提交于 2019-12-05 11:36:00
Very weird problem I've been struggling with for the past few hours (after solving 5-6 other issues with SFINAE as I'm new to it). Basically in the following code I want to have f() working for all possible template instantiations, but have g() available only when N == 2 : #include <type_traits> #include <iostream> template<typename T, int N> class A { public: void f(void); void g(void); }; template<typename T, int N> inline void A<T, N>::f() { std::cout << "f()\n"; } template<typename T, int N, typename std::enable_if<N == 2, void>::type* = nullptr> inline void A<T, N>::g() { std::cout << "g(

C++ Templates: Partial Template Specifications and Friend Classes

别来无恙 提交于 2019-12-05 03:34:51
is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class template <class T> class X{ T t; }; Now you have partial specializations, for example, for pointers template <class T> class X<T*>{ T* t; }; What I want to accomplish is that every possible X<T*> is a friend class of X<S> for ANY S . I.e. X<A*> should be a friend of X<B> . Of course, I thought about a usual template friend declaration in X: template <class T> class X{ template <class S> friend class X<S*>; } However, this does not compile, g++ tells me this: test4

Avoiding duplication of function definitions in template specializations

落爺英雄遲暮 提交于 2019-12-05 02:42:25
The class Widget has some functions that apply for all parameter types (common functions) and other functions that need to be specialized for given types (the uncommon functions). g++ insists that the specialization for Widget should also define common_fn() and not just uncommon_fn(), but that defeats the purpose of using specialization in the first place. How can one avoid repeating common_fn()? #include <cassert> template<typename Type> struct Widget { Widget() {} char common_fn() { return 'a'; } int uncommon_fn() { return 1; } }; template<> struct Widget<char> { Widget() {} int uncommon_fn(

Inner class depending on a template argument

会有一股神秘感。 提交于 2019-12-04 18:09:29
Consider next example : #include <iostream> #include <typeinfo> template< int N, typename T > struct B { struct C; }; template< typename T > struct B< 0, T >::C { typedef T type; }; template< int N, typename T > struct B< N, T >::C { typedef T type[N]; }; int main() { std::cout<<"n=0 type = " << typeid( B< 0, float >::C::type ).name() << std::endl; std::cout<<"n=5 type = " << typeid( B< 5, float >::C::type ).name() << std::endl; } When compiled using g++ (version 4.3.0) g++ dfg.cpp -ansi -pedantic -Wall the compile error is : dfg.cpp:13: error: qualified name does not name a class before ‘{’

Partial template specialization for specific type, c++

本秂侑毒 提交于 2019-12-04 16:53:46
Using partial specialization of templates I would like to create a function/method: A) processing only one specific primitive type (int, double, float,...) of the formal parameter and for other types throwing exception template <class T> T min ( Point <T> p ) { /*if (T == int) continue; else throw exception*/ } B) processing more non-primitive types (user defined types) of the formal parameter and for other types throwing exception... Some code examples would be helpful (without c++ boost library). Thanks for your help. This is the solution of your problem (part A). template<bool b> struct

Multiple SFINAE class template specialisations using void_t

笑着哭i 提交于 2019-12-04 15:39:13
问题 Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member typedef called "type". Here, a single specialisation is employed. This could be extended to identify say whether a type has either a member typedef called "type1", or one called "type2". The C++1z code below compiles with GCC, but not Clang. Is it

partial specialization for iterator type of a specified container type

℡╲_俬逩灬. 提交于 2019-12-04 11:58:35
I have a template struct, which accepts a Iterator type for the template argument. now I need to specialize that class for iterators of different containers. I have tried with std::vector template<typename Iterator> struct AC { }; template<typename T, typename Alloc> struct AC<typename std::vector<T, Alloc>::iterator> { //this doesn't work }; but I got this compiler error(VS11): 'T' : template parameter not used or deducible in partial specialization Can someone please tell me why this doesn't work? And how to make it work? You can't deduce types left of a nesting :: . Indeed, your question