template-specialization

specialize only (a part of) one method of a template class

余生长醉 提交于 2019-12-13 00:09:32
问题 If I have a template class template<typename T> class C { public: void method1() { ... } void method2() { ... } std::string method3(T &t) { // ... std::string s = t.SerializeToString(); // ... return s; } // ... }; and I want to specialize it for T = std::string but only changing method3(T&) (keeping all other methods), or even better, only that part of method3, which for T = std::string would simply become std::string s = t; , with minimum impact on current code (less repetition of methods

c++ scalable grouping of lambda functions in blocks of an arbitrary number

夙愿已清 提交于 2019-12-12 23:05:36
问题 I have to execute several lambda functions, but every each N lambdas a prologue() function also must be run. The number of lambdas can be arbitrary large and N is known at compile time. Something like this: static void prologue( void ) { cout << "Prologue" << endl; } int main() { run<3>( // N = 3 [](){ cout << "Simple lambda func 1" << endl; }, [](){ cout << "Simple lambda func 2" << endl; }, [](){ cout << "Simple lambda func 3" << endl; }, [](){ cout << "Simple lambda func 4" << endl; }, [](

What's the point of forward declaring a class template explicit/partial specialization?

﹥>﹥吖頭↗ 提交于 2019-12-12 18:26:53
问题 The C++98 standard says: [temp.class.spec] Partial specialization declarations themselves are not found by name lookup. If this is also true for explicit specializations, this makes a forward-declaration of a class template explicit/partial specialization invisible. [temp.class.spec.match] When a class template is used in a context that requires an instantiation of the class, it is necessary to determine whether the instantiation is to be generated using the primary template or one of the

How to write template overload functions with fallback triggered if template arguments do not allow instantiation of a certain class

柔情痞子 提交于 2019-12-12 17:09:35
问题 The program below does not compile if I uncomment the line containing foo<double>() , because B<double> depends on A<double> , which is an incomplete type. #include <iostream> using namespace std; template <class T> struct A; // forward declaration (incomplete) template <> struct A<int> {}; // specialized for int template <class T> struct B : A<T> { int foo() {return 0;} }; // derived class, general definition inherits from A template <> struct B<bool> { int foo() {return 1;} }; // derived

Template specialisation of a static function is shown in Doxygen-genered documentation

十年热恋 提交于 2019-12-12 17:07:47
问题 In a C++ header of my project, I am using few static template functions helpers which are called in the non-static public functions. When I run Doxygen on this file, each static function is hidden even when using templates, which is the correct behavior since static functions will not be visible for the outter-world. But when I do a template specialization on one of these, this specialization will appear in generated documentation : // This will not appear in documentation, ok template<class

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)

工具导航Map