specialization

Defining an Inner class member function template with a (non type) enum argument

只愿长相守 提交于 2019-12-10 13:37:22
问题 I'm having difficulty defining and specializing a member function update() of an inner class Outer<T1>::Inner that is templated on a non-type (enum) argument. #include <cstdlib> template<typename T1> struct Outer { struct Inner { enum Type{ A , B , C }; template<Type T2> void update(); }; }; // Definition template<typename T1> template<Outer<T1>::Inner::Type T2> void Outer<T1>::Inner::update() { } // Specialization template<typename T1> template<Outer<T1>::Inner::A > void Outer<T1>::Inner:

Size of generic type

懵懂的女人 提交于 2019-12-10 10:09:35
问题 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

Template specialization with a templatized type

一曲冷凌霜 提交于 2019-12-09 15:46:53
问题 I want to specialize a class template with the following function: template <typename T> class Foo { public: static int bar(); }; The function has no arguments and shall return a result based on the type of Foo. (In this toy example, we return the number of bytes of the type, but in the actual application we want to return some meta-data object.) The specialization works for fully specified types: // specialization 1: works template <> int Foo<int>::bar() { return 4; } // specialization 2:

Specialization of template after instantiation?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 19:13:49
问题 My full code is too long, but here is a snippet that will reflect the essence of my problem: class BPCFGParser { public: ... ... class Edge { ... ... }; class ActiveEquivClass { ... ... }; class PassiveEquivClass { ... ... }; struct EqActiveEquivClass { ... ... }; struct EqPassiveEquivClass { ... ... }; unordered_map<ActiveEquivClass, Edge *, hash<ActiveEquivClass>, EqActiveEquivClass> discovered_active_edges; unordered_map<PassiveEquivClass, Edge *, hash<PassiveEquivClass>,

Template << and >> operators specialization

谁说胖子不能爱 提交于 2019-12-08 05:23:07
问题 I want to template the >> and << operators in a class, but I would also like to specialize them for strings, so I did this; class sql_command { public: sql_command() { } explicit sql_command(std::string& cmd) : m_raw(cmd) { } inline sql_command& operator<<(const char * arg) { m_raw += arg; return *this; } inline sql_command& operator<<(const std::string& arg) { m_raw += arg; return *this; } template<typename T> inline sql_command& operator>>(const T arg) { m_raw += boost::lexical_cast<std:

C++ Partial Specialization ( Function Pointers )

☆樱花仙子☆ 提交于 2019-12-08 04:16:35
问题 Can any one please tell, whether below is legal c++ or not ? template < typename s , s & (*fn) ( s * ) > class c {}; // partial specialization template < typename s , s & (*fn) ( s * ) > class c < s*, s* & (*fn)(s**) {}; g++ ( 4.2.4) error: a function call cannot appear in a constant-expression error: template argument 2 is invalid Although it does work for explicit specialization int & func ( int * ) { return 0; } template <> class c < int , func> class c {}; 回答1: I think you mean template <

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

耗尽温柔 提交于 2019-12-07 15:22:27
问题 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?

C++ template specialization

北城余情 提交于 2019-12-07 13:13:22
问题 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; } }; 回答1: How about overloading struct Base { template <typename T> void Method(T a) { T b; } void

How to prevent specialization of std::vector<bool>

元气小坏坏 提交于 2019-12-06 19:04:52
问题 I have a templated class that has a data member of type std::vector<T> , where T is also a parameter of my templated class. In my template class I have quite some logic that does this: T &value = m_vector[index]; This doesn't seem to compile when T is a boolean, because the [] operator of std::vector does not return a bool-reference, but a different type. Some alternatives (although I don't like any of them): tell my users that they must not use bool as template parameter have a

Template << and >> operators specialization

我与影子孤独终老i 提交于 2019-12-06 15:12:48
I want to template the >> and << operators in a class, but I would also like to specialize them for strings, so I did this; class sql_command { public: sql_command() { } explicit sql_command(std::string& cmd) : m_raw(cmd) { } inline sql_command& operator<<(const char * arg) { m_raw += arg; return *this; } inline sql_command& operator<<(const std::string& arg) { m_raw += arg; return *this; } template<typename T> inline sql_command& operator>>(const T arg) { m_raw += boost::lexical_cast<std::string>(arg); return *this; } inline std::string const& command() const { return m_raw; } private: std: