template-specialization

Specialize function if argument has member variable

烈酒焚心 提交于 2019-12-12 12:32:46
问题 I have a function for error reporting that is templated because it can report errors for many different message classes: template <typename MSG> void reportErr(const MSG& msg) { std::cout << "ERROR: " << msg.error << std::endl; } However, some types of message have more detailed error that can be reported or other specialized error reporting, e.g. template<> void reportErr(const SpecificMsg& msg) { std::cout << "ERROR: " << msg.error; std::cout << ", details: " << msg.details << std::endl; }

Overriding a templated function with a polymorphic one

假如想象 提交于 2019-12-12 10:57:39
问题 If I have template<class T> TalkyBuffer& operator<<(T const &object) { // Template ... } TalkyBuffer& operator<<(TalkySerialisable const &object); // Override and a class class A : public TalkySerialisable { ...} Then if I perform TalkyBuffer b; A test; b << test; Then gcc is calling the Template function rather than the Override function However if I specifically define an override TalkyBuffer& operator<<(A const &object); // Override without polymorphism Then gcc picks that one. Is there a

Nested class template specialization

对着背影说爱祢 提交于 2019-12-12 10:28:52
问题 A class: template<typename C, typename T> class A { template <typename U> class Nested{}; Nested<T> n; }; And I want to specialize Nested . Here what I tried: template<typename C, typename T> class A { template <typename U> class Nested{}; template <> class Nested<int>{}; // by my logic this should work by I have a compilation error "explicit specialization in non-namespace scope 'class A<C, T>'" Nested<T> n; }; My next attempt: template<typename C, typename T> class A { template <typename U>

Serialization member by member

老子叫甜甜 提交于 2019-12-12 09:46:49
问题 I've implemented a template<typename T> Serializer that works on any trivially copyable object of type T , just serializing sizeof(T) bytes. Then I've implemented a couple of (partial) specializations for other types of interest, like std::vector<T> and std::bacis_string<T> . For other types, I trigger a static_assert(is_trivially_copyable<T>::type, "Unsupported type"); . This is not what I want, because I want to avoid serializing, for example, types with naked pointers , like: struct C

Define template specialization in cpp?

谁都会走 提交于 2019-12-12 08:23:41
问题 I can define a specialized function in a cpp like so... // header template<typename T> void func(T){} template<> void func<int>(int); // cpp template<> void func<int>(int) {} How can I define a method in a specialized class in a cpp? Like so (which doesn't work, I get error C2910: 'A<int>::func' : cannot be explicitly specialized )... // header template<typename T> struct A { static void func(T){} }; template<> struct A<int> { static void func(int); }; // cpp template<> void A<int>::func(int)

Template partial specialization for __stdcall function pointer

故事扮演 提交于 2019-12-12 07:52:03
问题 typedef bool (*my_function_f)(int, double); typedef bool (__stdcall *my_function_f2)(int, double); // ^^^^^^^^^ template<class F> class TFunction; template<class R, class T0, class T1> class TFunction<R(*)(T0,T1)> { typedef R (*func_type)(T0,T1); }; int main() { TFunction<my_function_f> t1; // works on x64 and win32 TFunction<my_function_f2> t2; // works on x64 and doesn't work on win32 return 0; } The code above gives me the following error in Visual C++ 2010: 1>e:\project\orwell\head

“ambiguating new declaration” error for a templated method in a templated class

烈酒焚心 提交于 2019-12-12 01:38:47
问题 I have written the following earth-shattering application: class SomeA { }; class SomeB { }; class SomeC { }; template <typename A, typename B, typename... Cs> class Foo { public: template <typename U> static void bar(); }; template <typename U> void Foo<SomeA, SomeB, SomeC>::bar() { }; int main() { return 0; } When I compile this (gcc 4.9.3 with -std=c++11 ), I get the following error: a.cpp:10:36: error: ambiguating new declaration of ‘static void Foo<SomeA, SomeB, SomeC>::bar()’ void Foo

Partial template specialization for more than one typename

筅森魡賤 提交于 2019-12-12 01:19:22
问题 In the following code, I want to consider functions ( Op s) that have void return to instead be considered to return true . The type Retval , and the return value of Op are always matching. I'm not able to discriminate using the type traits shown here, and attempts to create a partial template specialization based on Retval have failed due the presence of the other template variables, Op and Args . How do I specialize only some variables in a template specialization without getting errors? Is

Understanding an Implicit Instantiation of Class Template Specialization Example

﹥>﹥吖頭↗ 提交于 2019-12-11 16:46:38
问题 So this section of the standard gives this example (My questions are inline): template<class T, class U> struct Outer { template<class X, class Y> struct Inner; // Where are X and Y from? Is this defining a struct? template<class Y> struct Inner<T, Y>; // Is this defining a struct specialization? If so what is Y? template<class Y> struct Inner<T, Y> { }; // I feel like this is a redefinition of the line above, could it ever not be? template<class Y> struct Inner<U, Y> { }; }; Admittedly I can

Specialize function template with decltype trailing return type

我的梦境 提交于 2019-12-11 13:31:05
问题 In C++11, how can I specialise a function template which is declared with a "complicated" trailing return type using decltype? The following works in GCC but produces "error C2912: explicit specialisation 'int f(void)' i s not a specialisation of a function template" in VC2013: #include <iostream> int myint() { return 1; } template<class T> auto f() -> decltype(myint()) // this seems to cause problems { std::cout << "general\n"; return 1; } template <> auto f<double>() -> decltype(myint()) {