partial-specialization

Template Partial Specialization - any real-world example?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 08:21:03
问题 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! 回答1: 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,

partial specialization for iterator type of a specified container type

别来无恙 提交于 2019-12-21 20:13:27
问题 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

Partial template specialization ambiguity

喜欢而已 提交于 2019-12-21 07:15:14
问题 I cant see why the statement in main is ambiguous. template<class T, class U, int I> struct X { void f() { cout << "Primary template" << endl; } }; template<class T, int I> struct X<T, T*, I> {void f() { cout << "Partial specialization 1" << endl;}}; template<class T, class U, int I> struct X<T*, U, I> {void f() { cout << "Partial specialization 2" << endl;}}; template<class T> struct X<int, T*, 10> {void f() { cout << "Partial specialization 3" << endl;}}; template<class T, class U, int I>

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

和自甴很熟 提交于 2019-12-21 05:18:05
问题 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

Partial template specialization of free functions - best practices

十年热恋 提交于 2019-12-20 12:16:04
问题 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

Role of default template arguments in the context of partial specialization

五迷三道 提交于 2019-12-20 07:35:29
问题 I am not clear about the interaction of default template arguments in the context of partial specialization, for choosing which is the better matching template. This questions stems from code posted in this answer by max66. Given the definitions of the classes A and B : template <int N> struct A { static const int code = N; }; struct B{}; and the following template classes: // primary template template <typename, typename Enable = bool_constant<true>> struct cond : public bool_constant<false>

How to read the template partial specialization?

自古美人都是妖i 提交于 2019-12-20 02:43:06
问题 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

C++ partial template specialization syntax

岁酱吖の 提交于 2019-12-20 01:47:02
问题 for primary template: template<typename A, typename B> class MyClass {... with template specialization, what is the difference between template<typename A, typename B> class MyClass<int, float> {... and template<> class MyClass<int, float> {... 回答1: template<typename A, typename B> class MyClass<int, float> {... should be not allowed. Indeed, if you specify the formal parameters A and B , your template should be using them. The second case is just normal: you say that you are making

C++ partial template specialization syntax

让人想犯罪 __ 提交于 2019-12-20 01:46:11
问题 for primary template: template<typename A, typename B> class MyClass {... with template specialization, what is the difference between template<typename A, typename B> class MyClass<int, float> {... and template<> class MyClass<int, float> {... 回答1: template<typename A, typename B> class MyClass<int, float> {... should be not allowed. Indeed, if you specify the formal parameters A and B , your template should be using them. The second case is just normal: you say that you are making

Partial specialization of member function [duplicate]

会有一股神秘感。 提交于 2019-12-19 19:45:03
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: “invalid use of incomplete type” error with partial template specialization Why is it that I can do this: template <typename T> struct A { void foo(int); }; template <> void A<int>::foo(int) { } but not this: template <typename> struct C {}; template <typename T> struct A { void foo(int); }; template <typename T> void A<C<T> >::foo(int) { } For the second case, GCC gives the following error: test.cpp:10:23: