specialization

Partial template specialization for specific type, c++

时光怂恿深爱的人放手 提交于 2019-12-06 09:56:54
问题 Using partial specialization of templates I would like to create a function/method: A) processing only one specific primitive type (int, double, float,...) of the formal parameter and for other types throwing exception template <class T> T min ( Point <T> p ) { /*if (T == int) continue; else throw exception*/ } B) processing more non-primitive types (user defined types) of the formal parameter and for other types throwing exception... Some code examples would be helpful (without c++ boost

Size of generic type

房东的猫 提交于 2019-12-06 02:57:18
Is there any way to determine the size in bytes of something like TItem <T> = record Data : T; end; Can I write something like function TItem <T>.GetByteSize : Integer; begin if (T = String) then Result := GetStringByteSize (Data as String) else Result := SizeOf (Data); end; or perhaps with the help of specialization? function TItem <String>.GetByteSize : Integer; begin Result := GetStringByteSize (Data) end; function TItem <T>.GetByteSize : Integer; begin Result := SizeOf (Data); end; Thanks! Is there something wrong with taking the size of the instantiated type? SizeOf(TItem<string>) Or you

Overriding multiple inherited templated functions with specialized versions

你离开我真会死。 提交于 2019-12-06 02:26:12
问题 Okay, sample code first; this is my attempt at communicating what it is that I'm trying to do, although it doesn't compile: #include <iostream> template <class T> class Base { public: virtual void my_callback() = 0; }; class Derived1 : public Base<int> , public Base<float> { public: void my_callback<int>() { cout << "Int callback for Derived1.\n"; } void my_callback<float>() { cout << "Float callback for Derived\n"; } }; class Derived2 : public Base<int> , public Base<float> { public: void my

C++ template specialization

≯℡__Kan透↙ 提交于 2019-12-05 21:56:31
Hello! Does someone know a way to achieve or emulate the following behaviour? (this code results in compilation-time error). E.g, I want to add specific template specialization only in derived classes. struct Base { template <typename T> void Method(T a) { T b; } template <> void Method<int>(int a) { float c; } }; struct Derived : public Base { template <> void Method<float>(float a) { float x; } }; How about overloading struct Base { template <typename T> void Method(T a) { T b; } void Method(int a) { float c; } }; struct Derived : public Base { using Base::Method; void Method(float a) {

Why do these type arguments not conform to a type refinement?

≡放荡痞女 提交于 2019-12-05 21:34:44
Why does this Scala code fail to typecheck? trait T { type A } trait GenFoo[A0, S <: T { type A = A0 }] trait Foo[S <: T] extends GenFoo[S#A, S] I don't understand why "type arguments [S#A,S] do not conform to trait GenFoo's type parameter bounds [A0,S <: T{type A = A0}]". Is there a work-around? Edit : As has been pointed out, the conformance error stems from the failure to verify S <: T{type A = S#A} . Daniel Sobral pointed to -explaintypes , which tells us: S <: T{type A = S#A}? S <: T? true S specializes type A? this.A = this.A? S = this.type? false false false false I'm not sure how to

Partial Specialization of Operator()

落花浮王杯 提交于 2019-12-05 01:44:07
问题 One of my classes declares a templated function: template<class A, class B> A do_something(const std::vector<B> &data) which I'd like to partially specialize on typename A . B is a family of types that implement a pretty minimal interface, and we use a lot of them, so I'd like my specialization to be generic on B . I suspect this is doubly vexing as typename A is used only as the return type. From the internet, I've gleaned that I can't partially specialize a function, so I've created a class

How does trait specialization actually work?

醉酒当歌 提交于 2019-12-04 17:13:06
I tried to specialize a trait, and it fails to compile because of "conflicting implementations". But my understanding of specialization is that more specific implementations should override more generic ones. Here is a very basic example: mod diving { pub struct Diver<T> { inner: T } } mod swimming { use diving; pub trait Swimmer { fn swim(&self) { println!("swimming") } } impl<T> Swimmer for diving::Diver<T> { } } mod drowning { use diving; use swimming; impl swimming::Swimmer for diving::Diver<&'static str> { fn swim(&self) { println!("drowning, help!") } } } fn main() { let x = diving:

Partial template specialization for specific type, c++

本秂侑毒 提交于 2019-12-04 16:53:46
Using partial specialization of templates I would like to create a function/method: A) processing only one specific primitive type (int, double, float,...) of the formal parameter and for other types throwing exception template <class T> T min ( Point <T> p ) { /*if (T == int) continue; else throw exception*/ } B) processing more non-primitive types (user defined types) of the formal parameter and for other types throwing exception... Some code examples would be helpful (without c++ boost library). Thanks for your help. This is the solution of your problem (part A). template<bool b> struct

Template Specialization of Function inside of a Templated Class

烂漫一生 提交于 2019-12-04 14:31:38
问题 I have a templated class and inside I have a templated function( different template parameters ) and I having issues getting the compiler to call the correct one. Example: template< class Parm1, class Parm2, class Parm3 > class Class { public: void Func( Parm1 arg1, Parm2 arg2 ) { Call<Parm3>( arg1, arg2 ); } protected: template< class Type > void Call( Parm1 arg1, Parm2 arg2 ) { } template<> void Call<void>( Parm1 arg1, Parm2 arg2 ) { } }; So if the type of Parm3 is 'void' I want the second

Overriding multiple inherited templated functions with specialized versions

守給你的承諾、 提交于 2019-12-04 08:59:23
Okay, sample code first; this is my attempt at communicating what it is that I'm trying to do, although it doesn't compile: #include <iostream> template <class T> class Base { public: virtual void my_callback() = 0; }; class Derived1 : public Base<int> , public Base<float> { public: void my_callback<int>() { cout << "Int callback for Derived1.\n"; } void my_callback<float>() { cout << "Float callback for Derived\n"; } }; class Derived2 : public Base<int> , public Base<float> { public: void my_callback<int>() { cout << "Int callback for Derived2.\n"; } void my_callback<float>() { cout << "Float