partial-specialization

Is it legal to perform partial in-class specialization of a member template class in derived class

我只是一个虾纸丫 提交于 2019-12-04 04:30:19
It is continuation of this question. I am specifically interested if the partial specialization of a member class like this: struct FooParent { template <class> struct Bar{ }; }; struct Foo: FooParent { template <class T> struct Bar<T*> {}; }; I know this can be done inside a namespace scope: template <class T> struct Foo::Bar<T*>{ }; But I'm also specifically interested in in-class partial specialization at the level of derived class. Both clang and gcc complains when encounter a former: clang states that there is an explicit template specialization which obviously does not occur: error:

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

跟風遠走 提交于 2019-12-04 04:30:00
问题 #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'. 回答1: Template specialization can't be used

Partial specialisation of member function with non-type parameter

北城余情 提交于 2019-12-04 03:20:18
I have a template class with both a type and a non-type template parameter. I want to specialize a member function, what I finding is, as in the example below, I can do a full specialization fine. template<typename T, int R> struct foo { foo(const T& v) : value_(v) {} void bar() { std::cout << "Generic" << std::endl; for (int i = 0; i < R; ++i) std::cout << value_ << std::endl; } T value_; }; template<> void foo<float, 3>::bar() { std::cout << "Float" << std::endl; for (int i = 0; i < 3; ++i) std::cout << value_ << std::endl; } However this partial specialization won't compile. template<int R>

Can I use partial template specialization for a (non-member) function?

不问归期 提交于 2019-12-03 16:34:35
I'm trying to use partial template specialization on a (non-member) function, and I'm tripping up on the syntax. I've searched StackOverflow for other partial template specialization questions, but those deal with partial specialization of a class or member function template. For a starting point, I have: struct RGBA { RGBA(uint8 red, uint8 green, uint8 blue, uint8 alpha = 255) : r(red), g(green), b(blue), a(alpha) {} uint8 r, g, b, a; }; struct Grayscale { Grayscale(uint8 intensity) : value(intensity) {} uint8 value; }; inline uint8 IntensityFromRGB(uint8 r, uint8 g, uint8 b) { return static

Multiple SFINAE class template specialisations using void_t

大城市里の小女人 提交于 2019-12-03 09:42:12
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 legal? template <class, class = std::void_t<>> struct has_members : std::false_type {}; template

Get the signed/unsigned variant of an integer template parameter without explicit traits

杀马特。学长 韩版系。学妹 提交于 2019-12-03 04:55:15
I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T , and the other as the unsigned variant of type T -- i.e. if T == int , then T_Unsigned == unsigned int . My first instinct was to do this: template <typename T> class Range { typedef unsigned T T_Unsigned; // does not compile public: Range(T min, T_Unsigned range); private: T m_min; T_Unsigned m_range; }; But it doesn't work. I then thought about using partial template specialization, like so: template <typename T> struct UnsignedType {}; //

Partial template specialization of free functions - best practices

时光怂恿深爱的人放手 提交于 2019-12-03 02:27:29
As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++: template <class T, int N> T mul(const T& x) { return x * N; } template <class T> T mul<T, 0>(const T& x) { return T(0); } // error: function template partial specialization ‘mul<T, 0>’ is not allowed However, partial template specialization of classes/structs is allowed, and can be exploited to mimic the functionality of partial template specialization of free functions. For example, the target objective in the last example can be achieved by using:

partial specialization with dependent name (typename)

眉间皱痕 提交于 2019-12-02 08:08:34
问题 I have the following simple strinToTypeImpl function which converts any kind of string into the template type. The problem I am concerned about is the fact that the compiler tells me for the partial specialization for typename MyMatrix<T>::Vector3 : template parameter T not used in partial specialization Can't I use dependent names in the specialization? namespace details { template<typename T> struct stringToTypeImpl{ bool operator()(T& t, const std::string& s) { std::istringstream iss(s);

partial specialization with dependent name (typename)

纵然是瞬间 提交于 2019-12-02 04:44:55
I have the following simple strinToTypeImpl function which converts any kind of string into the template type. The problem I am concerned about is the fact that the compiler tells me for the partial specialization for typename MyMatrix<T>::Vector3 : template parameter T not used in partial specialization Can't I use dependent names in the specialization? namespace details { template<typename T> struct stringToTypeImpl{ bool operator()(T& t, const std::string& s) { std::istringstream iss(s); return !(iss >> t).fail(); } }; template<typename T> struct stringToTypeImpl< typename MyMatrix<T>:

How to read the template partial specialization?

谁都会走 提交于 2019-12-01 22:04:31
Suppose the following declaration: template <typename T> struct MyTemplate; The following definition of the partial specialization seems to use the same letter T to refer to different types. template <typename T> struct MyTemplate<T*> {}; For example, let's take a concrete instantiation: MyTemplate<int *> c; Now, consider again the above definition of the partial specialization: template <typename T> struct MyTemplate<T*> {}; In the first part of this line (i.e. template <typename T> ), T is int * . In the second part of the line (i.e. MyTemplate<T*> ), T is int ! So, how is the definition of