specialization

Template specialization based on inherit class

可紊 提交于 2019-11-27 06:59:54
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so. -edit- I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them. class SomeTag {}; class InheritSomeTag : public SomeTag {}; template <class T, class Tag=T> struct MyClass { }; template <class T> struct MyClass<T, SomeTag> { typedef int isSpecialized; }; int main() { MyClass<SomeTag>::isSpecialized test1; //ok MyClass<InheritSomeTag>::isSpecialized test2; //how do i make this specialized w/o changing main()

Template specialization of a single method from a templated class

China☆狼群 提交于 2019-11-27 06:28:35
Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly: template <class T> class TClass { public: void doSomething(std::vector<T> * v); }; template <class T> void TClass<T>::doSomething(std::vector<T> * v) { // Do something with a vector of a generic T } template <> inline void TClass<int>::doSomething(std::vector<int> * v) { // Do something with a vector of int's } But note the inline in the specialization method. It is required to avoid a linker error (in VS2008 is LNK2005) due to the method being

Selecting a member function using different enable_if conditions

谁说胖子不能爱 提交于 2019-11-27 04:26:24
I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this: #include <iostream> #include <type_traits> template<typename T> struct Point { void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0) { std::cout << "T is int." << std::endl; } void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0) { std::cout << "T is not int." << std::endl; } }; int main() { Point<int> intPoint; intPoint.MyFunction(); Point<float> floatPoint; floatPoint.MyFunction(); } which I

C++ single template specialisation with multiple template parameters

霸气de小男生 提交于 2019-11-27 02:20:38
问题 Hallo! I would like to specialise only one of two template types. E.g. template <typename A, typename B> class X should have a special implementation for a single function X<float, sometype>::someFunc() . Sample code: 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

template class member function only specialization

非 Y 不嫁゛ 提交于 2019-11-26 19:57:52
问题 I am reading the Complete Guide on Templates and it says the following: Where it is talking about class template specialization. However, if you specialize a class template, you must also specialize all member functions. Although it is possible to specialize a single member function, once you have done so, you can no longer specialize the whole class. I'm actually wondering how this is true, cause you can specialize without any member functions at all. Is it saying that you cannot have a

Default template parameter partial specialization

≯℡__Kan透↙ 提交于 2019-11-26 19:48:24
问题 Please explain to me why the following piece of code complies and works perfectly. I am very confused. #include<iostream> template<class A = int, class B=double> class Base {}; template<class B> class Base <int, B> { public: Base() { std::cout<<"it works!!!!!\n"; } }; int main() { Base<> base; // it prints "it works!!!!!" return 0; } Shouldn't it fall into the generalized form of the template class Base? 回答1: The default argument applies to the specialization -- and, in fact, a specialization

Specializing a template on a lambda in C++0x

给你一囗甜甜゛ 提交于 2019-11-26 19:24:31
问题 I've written a traits class that lets me extract information about the arguments and type of a function or function object in C++0x (tested with gcc 4.5.0). The general case handles function objects: template <typename F> struct function_traits { template <typename R, typename... A> struct _internal { }; template <typename R, typename... A> struct _internal<R (F::*)(A...)> { // ... }; typedef typename _internal<decltype(&F::operator())>::<<nested types go here>>; }; Then I have a

Template specialization of particular members?

假装没事ソ 提交于 2019-11-26 17:32:15
Is it possible to specialize particular members of a template class? Something like: template <typename T,bool B> struct X { void Specialized(); }; template <typename T> void X<T,true>::Specialized() { ... } template <typename T> void X<T,false>::Specialized() { ... } Ofcourse, this code isn't valid. You can only specialize it explicitly by providing all template arguments. No partial specialization for member functions of class templates is allowed. template <typename T,bool B> struct X { void Specialized(); }; // works template <> void X<int,true>::Specialized() { ... } A work around is to

c++ template partial specialization member function [duplicate]

一曲冷凌霜 提交于 2019-11-26 16:32:29
This question already has an answer here: “invalid use of incomplete type” error with partial template specialization 3 answers I'm new to templates so maybe this is a trivial thing but I cannot get it to work. I'm trying to get partial specialization of a class member function. The shortest code would be: template <typename T, int nValue> class Object{ private: T m_t; Object(); public: Object(T t): m_t(t) {} T Get() { return m_t; } Object& Deform(){ m_t*=nValue; return *this; } }; template <typename T> Object<T,0>& Object<T,0>::Deform(){ this->m_t = -1; return *this; } int main(){ Object<int

static member initialization for specialized template class

半世苍凉 提交于 2019-11-26 16:25:52
问题 class A { }; template <typename A, int S> class B { public: static int a[S]; B() { a[0] = 0; } }; template<> int B<A, 1>::a[1]; int main() { B<A, 1> t; t; } It compiles under GCC 4.1, but does not link: static.cpp:(.text._ZN1BI1ALi1EEC1Ev[B<A, 1>::B()]+0x5): undefined reference to `B<A, 1>::a' I would prefer to keep initialisation specialised if it is possible, since the array holds some data specific to the type. 回答1: For static member specializations, if you don't initialize the member, it