specialization

template specialization of template class

房东的猫 提交于 2019-12-01 01:35:38
问题 I want to specialize following member function: class foo { template<typename T> T get() const; }; To other class bar that depends on templates as well. For example, I would like bar to be std::pair with some template parameters, something like that: template<> std::pair<T1,T2> foo::get() const { T1 x=...; T2 y=...; return std::pair<T1,T2>(x,y); } Where T1 and T2 are templates as well. How can this be done? As far as I know it should be possible. So now I can call: some_foo.get<std::pair<int

Function template specialization with a template class [duplicate]

两盒软妹~` 提交于 2019-11-30 15:38:29
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<> print<int>(int value) { std::cout << value; } But now the problem, I want it to work with a vector as well. Since the vector class is a template class it becomes

Partial specialization of double-templated method fails

╄→尐↘猪︶ㄣ 提交于 2019-11-30 14:19:43
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> ( const char *file ) { } Error 3 error C2768: 'List<Point>::load' : illegal use of explicit template

How can I get a specialized template to use the unspecialized version of a member function?

人走茶凉 提交于 2019-11-30 10:47:41
Consider the following code: template <int dim> struct vec { vec normalize(); }; template <> struct vec<3> { vec cross_product(const vec& second); vec normalize(); }; template <int dim> vec<dim> vec<dim>::normalize() { // code to normalize vector here return *this; } int main() { vec<3> direction; direction.normalize(); } Compiling this code produces the following error: 1>main.obj : error LNK2019: unresolved external symbol "public: struct vec<3> __thiscall vec<3>::normalize(void)" (?normalize@?$vec@$02@@QAE?AU1@XZ) referenced in function _main You can't :) What you want is to specialize the

How can one provide manually specialized implementations with Scala specialization?

谁都会走 提交于 2019-11-30 04:38:20
Specialization promises to provide high-efficiency implmentations for primitive types with minimal extra boilerplate. But specialization seems to be too eager for its own good. If I want to specialize a class or method, def foo[@specialized(Byte) A](a: A): String = ??? class Bar[@specialized(Int) B] { var b: B = ??? def baz: B = ??? } then I am required to write a single implementation that covers both the specialized and the generic cases. What if those cases are really different from each other, such that the implementations do not overlap? For example, if I wanted to perform math on bytes,

Template specialization of a single method from templated class with multiple template parameters

故事扮演 提交于 2019-11-29 15:24:26
I'm basically trying to do what was discussed in Template specialization of a single method from a templated class except that my TClass has multiple template Parameters like this: template < class KEY, class TYPE > class TClass { public: : void doSomething(KEY * v); : }; template < class KEY, class TYPE > void TClass<KEY, TYPE>::doSomething(KEY * v) { // do something } This works so far, but how do I define a specialized implementation for one template Parameter? I tried adding this: template < class TYPE > void TClass<int, TYPE>::doSomething(int * v) { // do something if KEY is int } but the

Is partial class template specialization the answer to this design problem?

烂漫一生 提交于 2019-11-29 09:39:55
问题 Say you have a class who's job it is to connect to a remote server. I want to abstract this class to provide two versions, one that connects through UDP and the other through TCP. I want to build the leanest runtime code possible and instead of using polymorphism I am considering templates. Here is what I'm envisioning but I'm not sure it's the best way of doing this: class udp {}; class tcp {}; template<class T,typename X> class service { private: // Make this private so this non specialized

Specializing function template for reference types

不羁岁月 提交于 2019-11-29 09:26:55
Why is the output of this code : #include <iostream> template<typename T> void f(T param) { std::cout << "General" << std::endl ; } template<> void f(int& param) { std::cout << "int&" << std::endl ; } int main() { float x ; f (x) ; int y ; f (y) ; int& z = y ; f (z) ; } is General General General The third one is surprizing because the function was specialized exactly for int& Edit : I know that overloading might be a proper solution. I just want to learn the logic behind it. The type of both the expression y and the expression z is int . A reference appearing in an expression won't keep

How can one provide manually specialized implementations with Scala specialization?

随声附和 提交于 2019-11-29 01:54:43
问题 Specialization promises to provide high-efficiency implmentations for primitive types with minimal extra boilerplate. But specialization seems to be too eager for its own good. If I want to specialize a class or method, def foo[@specialized(Byte) A](a: A): String = ??? class Bar[@specialized(Int) B] { var b: B = ??? def baz: B = ??? } then I am required to write a single implementation that covers both the specialized and the generic cases. What if those cases are really different from each

篮子、水果和鸡蛋——关于C++的模板偏特化和萃取编程技法

江枫思渺然 提交于 2019-11-29 01:48:52
最近在读《STL源码剖析》。读这本书的时候发现自己的C++的知识其实是非常匮乏的。 从大学的C++教材上学到一些C++基本的语法、内存管理、继承、多态等方面的基础知识。这些只是是一棵大树的根。而读STL的源码和侯捷的解析的时候,发现C++还有很多丰富的细节和技巧。这些是大树上的枝叶。学习C++,不仅要学习根,也要学习枝叶,这样才能让大树茂盛起来。虽然C++语法一些用法较为晦涩,但读完这些代码之后觉得思路比以前更开阔,另外可以活动脑筋。 比如模板的偏特化这个特性。侯捷的《STL源码剖析》中对于模板的偏特化(partial specialization)的解释为: 如果class template拥有一个以上的template参数,我们可以针对其中某个或多个 template参数进行特化工作。template是一个很抽象的东西。template偏特化之后就让模板变得具体那么一点点。 用一个形象一点的比喻吧。我们把template比作一个装东西的篮子。这个篮子既可以装鸡蛋,也可以装苹果。那么所谓偏特化就是让你用一个篮子专门装水果,这就是template水果篮。以后你就只能使用水果篮来装苹果、装梨,而不能使用其他的篮子来装这些水果了。 看一个STL例子: 有一个“篮子” iterator_traits, 它内部typedef 了value_type类型,用来定义模板的参数类型class I