partial-specialization

Partial specialization of function templates

对着背影说爱祢 提交于 2019-11-27 13:36:06
Does anyone know whether, in C++11, function templates can be partially specialized? No, they can't. The draft C++0x standard has a section (14.5.5) on class template partial specialisations, but no mention of function template partial specialisations. Lightness Races in Orbit No ; they were proposed as core language issue #229 (from n1295 ) but ultimately rejected (and quite rightly so, since overloading does the job). 来源: https://stackoverflow.com/questions/3716799/partial-specialization-of-function-templates

Template parameters not used in partial specialization

随声附和 提交于 2019-11-27 08:01:44
问题 I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray { // ... typedef T* pointer; typedef pointer iterator; // ... }; Now I'm trying to do partial specialization for iterator_traits . It seems OK to me, but g++ 4.4.5 complains: #include <iterator> namespace std { template<typename T, typename Allocator> struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128 typedef T value_type; typedef typename Allocator::difference_type

(Partially) specializing a non-type template parameter of dependent type

偶尔善良 提交于 2019-11-27 07:16:56
Maybe I'm tired, but I'm stuck with this simple partial specialization, which doesn't work because non-type template argument specializes a template parameter with dependent type 'T' : template <typename T, T N> struct X; template <typename T> struct X <T, 0>; Replacing 0 by T(0) , T{0} or (T)0 doesn't help. So is this specialization even possible? See paragraph [temp.class.spec] 14.5.5/8 of the standard: The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. [ Example: template <class T, T t> struct C {};

Tag dispatch versus static methods on partially specialised classes

回眸只為那壹抹淺笑 提交于 2019-11-26 23:51:56
问题 Suppose I want to write a generic function void f<T>() , which does one thing if T is a POD type and another thing if T is non-POD (or any other arbitrary predicate). One way to achieve this would be to use a tag-dispatch pattern like the standard library does with iterator categories: template <bool> struct podness {}; typedef podness<true> pod_tag; typedef podness<false> non_pod_tag; template <typename T> void f2(T, pod_tag) { /* POD */ } template <typename T> void f2(T, non_pod_tag) { /*

Partial specialization of function templates

邮差的信 提交于 2019-11-26 22:23:14
问题 Does anyone know whether, in C++11, function templates can be partially specialized? 回答1: No, they can't. The draft C++0x standard has a section (14.5.5) on class template partial specialisations, but no mention of function template partial specialisations. 回答2: No; they were proposed as core language issue #229 (from n1295) but ultimately rejected (and quite rightly so, since overloading does the job). 来源: https://stackoverflow.com/questions/3716799/partial-specialization-of-function

partial specialization ordering with non-deduced context

时光总嘲笑我的痴心妄想 提交于 2019-11-26 20:59:36
问题 According to [temp.class.order] §14.5.5.2, the selection of a partial specialization of t in this example: template< typename > struct s { typedef void v, w; }; template< typename, typename = void > struct t {}; template< typename c > struct t< c, typename c::v > {}; template< typename c > struct t< s< c >, typename s< c >::w > {}; t< s< int > > q; is equivalent to the selection of an overload of f in this example: template< typename > struct s { typedef void v, w; }; template< typename,

Why is it disallowed for partial specialization in a non-type argument to use nested template parameters

三世轮回 提交于 2019-11-26 20:06:48
问题 I have this code template<int N, bool C = true> struct A; template<int N> struct A<N, !(N % 5)> { /* ... */ }; // should work A<25> a; That is, for numbers N that are divisible by 5 , the compiler should use the partial specialization. But the compiler won't accept that partial specialization, because the Standard requires it to reject such code where a non-type argument of a partial specialization references a parameter and is not simply a parameter (like, A<N, N> would be valid). But what

“invalid use of incomplete type” error with partial template specialization

北城以北 提交于 2019-11-26 17:47:55
The following code: template <typename S, typename T> struct foo { void bar(); }; template <typename T> void foo <int, T>::bar() { } gives me the error invalid use of incomplete type 'struct foo<int, T>' declaration of 'struct foo<int, T>' (I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the second argument: template <typename S> struct foo { void bar(); }; template <> void foo <int>::bar() { } then it compiles correctly. You can't partially specialize a function. If you wish to do so on a member function, you must partially specialize the entire template

Why function template cannot be partially specialized?

旧街凉风 提交于 2019-11-26 15:58:58
I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! Cheers and hth. - Alf AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member

How to do template specialization in C#

雨燕双飞 提交于 2019-11-26 15:58:40
How would you do specialization in C#? I'll pose a problem. You have a template type, you have no idea what it is. But you do know if it's derived from XYZ you want to call .alternativeFunc() . A great way is to call a specialized function or class and have normalCall return .normalFunc() while have the other specialization on any derived type of XYZ to call .alternativeFunc() . How would this be done in C#? Marc Gravell In C#, the closest to specialization is to use a more-specific overload; however, this is brittle, and doesn't cover every possible usage. For example: void Foo<T>(T value)