template-specialization

Call non-specialised template class function from specialized template class function

六月ゝ 毕业季﹏ 提交于 2019-12-23 07:25:50
问题 Is it possible to call a function defined in a non-specialised template class from a specialised template class? Here is an example of what i am attempting: template <typename T> struct Convert { static inline void toString(unsigned num, unsigned places, std::string& str) { ... } }; template <> struct Convert<int8_t> { static inline void toString(unsigned num, std::string& str) { Convert<int8_t>::toString(num, digitis(num), str); } }; GCC complains that it can't see the non-specialised class

When specializing a class, how can I take a different number of template parameters?

不羁岁月 提交于 2019-12-23 04:09:22
问题 I just asked this question: Can I get the Owning Object of a Member Function Template Parameter? and Yakk - Adam Nevraumont's answer had the code: template<class T> struct get_memfun_class; template<class R, class T, class...Args> struct get_memfun_class<R(T::*)(Args...)> { using type=T; }; These is clearly an initial declaration and then a specialization of struct get_memfun_class . But I find myself uncertain: Can specializations have a different number of template parameters? For example,

How to specialize a templated member-function into a templated class in C++?

二次信任 提交于 2019-12-23 03:14:34
问题 With regards to this question: How to create specialization for a single method in a templated class in C++? ... I have this class: template <typename T> class MyCLass { public: template <typename U> U myfunct(const U& x); }; // Generic implementation template <typename T> template <typename U> U MyCLass<T>::myfunct(const U& x) {...} And I want to specialize myfunct for double s. This is what I do: // Declaring specialization template <> template <typename T> double MyCLass<T>::myfunct(const

Explicit specialization incurs instantiation?

守給你的承諾、 提交于 2019-12-23 02:48:07
问题 Answering this question, I got a surprising error from GCC and Clang: template< typename = void > struct Outer_temp { struct Inner; Inner myinner; }; template<> struct Outer_temp<void>::Inner // Error: specialization of Outer_temp { // with member myinner of incomplete type. }; Why does declaring an explicit specialization require implicit instantiation? Does this fall into the same category as all uses of the scope resolution operator? Instantiating the template to find the declaration of a

Specializations only for C++ template function with enum non-type template parameter

橙三吉。 提交于 2019-12-23 00:52:31
问题 This question is related to this one except that rather than dealing with typename template parameters, I am trying to use an enum non-type template parameter. Is it possible to have a templated (class member function) with only specializations, no general (working) definition in the case of non-type template parameter? I was able to get one version working, by declaration in the class body and providing specializations only, but any misuse calling with a non-defined template parameter doesn

Explicit template specialization

女生的网名这么多〃 提交于 2019-12-22 10:26:41
问题 I hate to ask such a general question, but the following code is a exercise in explicit template specialization. I keep getting the error: c:\users\***\documents\visual studio 2010\projects\template array\template array\array.h(49): error C2910: 'Array::{ctor}' : cannot be explicitly specialized #ifndef ARRAY_H #define ARRAY_H template <typename t>` class Array { public: Array(int); int getSize() { return size; } void setSize(int s) { size = s; } void setArray(int place, t value) { myArray

choose correct template specialization at run-time

此生再无相见时 提交于 2019-12-22 06:51:32
问题 I have template <int i> struct a { static void f (); }; with specializations done at different places in the code. How can I call the correct a<i>::f for an i known only at runtime? void f (int i) { a<i>::f (); } // won't compile I don't want to list all possible values of i in a big switch . Edit: I thought of something like #include <iostream> template <int i> struct a { static void f (); }; struct regf { typedef void (*F)(); enum { arrsize = 10 }; static F v[arrsize]; template < int i >

Why does this code give the error, “template specialization requires 'template<>'”?

人盡茶涼 提交于 2019-12-22 04:39:25
问题 When I try to compile this with Clang template<class T> struct Field { char const *name; Field(char const *name) : name(name) { } }; template<class Derived> class CRTP { static Field<Derived> const _field; }; class Class : public CRTP<Class> { }; Field<Class> const CRTP<Class>::_field("blah"); int main() { } I get error: template specialization requires 'template<>' Field<Class> const CRTP<Class>::_field("blah"); ~~~~~~~~~~~ ^ I don't understand the error at all. What is wrong with my

How to extract the highest-indexed specialization from a structure?

给你一囗甜甜゛ 提交于 2019-12-22 03:46:53
问题 I'm trying to do some template metaprogramming and I'm finding the need to "extract" the highest index of a specialization of some structure in some type. For example, if I have some types: struct A { template<unsigned int> struct D; template<> struct D<0> { }; }; struct B { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; }; struct C { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; template<> struct D<2> { }; }

Specializing a template member function of a template class?

左心房为你撑大大i 提交于 2019-12-21 16:53:45
问题 I have a template class that has a template member function that needs to be specialized, as in: template <typename T> class X { public: template <typename U> void Y() {} template <> void Y<int>() {} }; Altough VC handles this correctly, apperantly this isn't standard and GCC complains: explicit specialization in non-namespace scope 'class X<T>' I tried: template <typename T> class X { public: template <typename U> void Y() {} }; template <typename T> // Also tried `template<>` here void X<T>