template-specialization

Template class incomplete specialization

你说的曾经没有我的故事 提交于 2019-11-26 21:39:28
问题 I came across an interesting point that I wasn't able to explain or find an explanation for. Consider the following template definition (compiled with mingw g++ 4.6.2): template <typename T, typename S> class Foo { public: void f(){} void g(){} }; Should we want to, we can fully specialize any single member function: template <> void Foo<char,int>::f() {} But partial specialization fails with an "invalid use of incomplete type 'class Foo<...>'" error: template <typename T, typename S> void

Function template specialization importance and necessity

≯℡__Kan透↙ 提交于 2019-11-26 20:45:08
问题 I read C++ Primer, and it says function template specialization is an advanced topic, but I am totally lost. Can anybody offer an example why function template specialization is important and necessary? Why don't function templates support partial specialization while class templates do? What's the underlying logic? 回答1: Basically the idea is that you can write templates that behave in a generic way for the general case, but can still handle special cases. One example of where specialization

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

C++ function template partial specialization?

女生的网名这么多〃 提交于 2019-11-26 12:57:03
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 Express! If no, then could you please explain the partial specialization concept? #include <iostream>

C++ templates specialization syntax

被刻印的时光 ゝ 提交于 2019-11-26 08:48:12
问题 In C++ Primer Plus (2001, Czech Translation) I have found these different template specialization syntax: function template template <typename T> void foo(T); specialization syntax void foo(int param); // 1 void foo<int>(int param); // 2 template <> void foo<int>(int param); // 3 template <> void foo(int param); // 4 template void foo(int param); // 5 Googling a bit, I have found only No.3 examples. Is there any difference (in call, compiling, usage) among them? Are some of them obsolete

Specialization of templated member function in templated class

可紊 提交于 2019-11-26 08:29:10
问题 I have a templated class with an templated member function template<class T> class A { public: template<class CT> CT function(); }; Now I want to specialize the templated member function in 2 ways. First for having the same type as the class: template<class T> template<> // Line gcc gives an error for, see below T A<T>::function<T>() { return (T)0.0; } Second for type bool: template<class T> template<> bool A<T>::function<bool>() { return false; } Here is how I am trying to test it: int main(

Template Specialization VS Function Overloading

泄露秘密 提交于 2019-11-26 01:46:50
问题 A textbook I have notes that you can provide your own implementation for standard library functions like swap(x,y) via template specialization or function overloading. This would be useful for any types which can benefit from something other than an assignment swap, like STL containers for example (which already have swaps written, I know). My questions are the following: What\'s better: template specialization to give your specialized swap implementation, or function overloading providing