partial-specialization

Partial specialization of member function [duplicate]

大城市里の小女人 提交于 2019-12-19 19:44:35
问题 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:

Function template specialization with a template class [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-18 17:25:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: partial specialization of function template I can't find anywhere a solution for my problem, because if I search with the keywords I come up with would give me solutions suited for different problems. I understand that this must been asked before, just can't find a solution. Suppose I have a function template: template<class any> print(any value); I can specialize it like this for let's say a int : template<>

Partial specialization of double-templated method fails

你说的曾经没有我的故事 提交于 2019-12-18 16:33:09
问题 There is the template class List. template <typename Point> class List { public: template <const unsigned short N> void load ( const char *file); ... }; template <typename Point> template <const unsigned short N> void List <Point>::load ( const char *file) } How to specialize method load for N=2? This code is not valid... template <typename Point> void List <Point> <2>::load ( const char *file) { } And this code also does not work. template <typename Point> void List <Point> ::load <2> (

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

时光怂恿深爱的人放手 提交于 2019-12-17 04:05: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. 回答1: You can't partially

Why function template cannot be partially specialized?

最后都变了- 提交于 2019-12-17 03:03:01
问题 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! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

Why function template cannot be partially specialized?

混江龙づ霸主 提交于 2019-12-17 03:02:47
问题 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! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

How to do template specialization in C#

筅森魡賤 提交于 2019-12-17 03:01:05
问题 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#? 回答1: In C#, the closest to specialization is to use a more-specific overload; however

C++ function template partial specialization?

泪湿孤枕 提交于 2019-12-17 02:35:10
问题 I know that the below code is a partial specialization of a class: template <typename T1, typename T2> class MyClass { … }; // partial specialization: both template parameters have same type template <typename T> class MyClass<T,T> { … }; Also I know that C++ does not allow function template partial specialization (only full is allowed). But does my code mean that I have partially specialized my function template for one/same type arguments? Because it works for Microsoft Visual Studio 2010

Undefined reference to partial specialized template class function during link time

早过忘川 提交于 2019-12-13 03:59:01
问题 So I had an problem with partial specialization of function templates. I choose the solution described here: Question Now I have this: #include <vector> #include <iostream> template <typename T> struct helper { static void print(T value) { std::cout << value; } }; template <typename T> struct helper<std::vector<T>> { static void print(std::vector<T> const &value) { } }; template <typename T> void print (T const &value) { // Just delegate. helper<T>::print (value); } int main () { print (5);

Avoiding duplication of function definitions in partial template specializations when using traits

孤街浪徒 提交于 2019-12-12 13:25:47
问题 How does one share common_fn() among all specializations (for Widget<A<T> > and Widget<B<T> > , no matter what T is) in the code below? #include <cassert> struct Afoo {}; struct Bfoo {}; template<typename T> struct A { typedef Afoo Foo; }; template<typename T> struct B { typedef Bfoo Foo; }; template<typename Type> struct Widget { Widget() {} typename Type::Foo common_fn() { return Type::Foo(); } int uncommon_fn() { return 1; } }; template<typename T> struct Widget<A<T> > { Widget() {} int