template-specialization

Template specialization (boost::lexical_cast)

我怕爱的太早我们不能终老 提交于 2019-12-13 17:33:28
问题 I want to extend the lexical_cast method for vector<uint> types, but it's not working. I tried the following code: #include <boost/lexical_cast.hpp> namespace boost { template <> inline string lexical_cast <string>(vector<uint> source) { string tmp; for (size_t i = 0; i < source.size(); ++i) if (i < source.size() - 1) tmp += boost::lexical_cast<string>(source[i]) + "|"; else tmp += boost::lexical_cast<string>(source[i]); return tmp; } } I got following error: error: template-id ‘lexical_cast’

gcc does not find template specialization

て烟熏妆下的殇ゞ 提交于 2019-12-13 16:22:04
问题 My network code uses template specialization to serialize types that can not simply be copied. I defined a general template template<typename T> struct TypeHandler that handles all types that can be transported by a simple memcpy and then I define specializations for all other types. The problem now is that I have a file with multiple such specializations and if I compile the code with Visual Studio everything works fine. But with gcc all template specializations in that file get used with

Partial template specialization with non-type parameters: GCC vs MSVS

╄→尐↘猪︶ㄣ 提交于 2019-12-13 15:24:11
问题 Consider this simple template specialization: template<typename T, size_t I> struct S {}; template<typename T> struct S<T, std::tuple_size<T>::value> {}; GCC does not compile it, as it uses template parameter T in the template argument std::tuple_size<T>::value : error: template argument 'std::tuple_size<_Tp>::value' involves template parameter(s) Now let's replace T with typename std::remove_reference<T>::type in tuple_size template argument: // Using primary structure template from previous

Is Clang correct to reject code in which the nested class of a class template is defined only via specializations?

…衆ロ難τιáo~ 提交于 2019-12-13 12:24:02
问题 Given the following class template: template<typename T> struct Outer { struct Inner; auto f(Inner) -> void; }; we define Inner separately for each specialization of Outer : template<> struct Outer<int>::Inner {}; template<> struct Outer<double>::Inner {}; and then define the member function f once for all specializations of Outer : auto Outer<T>::f(Inner) -> void { } but Clang (9.0.0) complains: error: variable has incomplete type 'Outer::Inner' auto Outer<T>::f(Inner) -> void ^ We can evade

Syntax for partial class template specialization

狂风中的少年 提交于 2019-12-13 05:18:30
问题 In the following, am I forgetting some correct syntax for partial specializing class NumInfo or is it even possible to do that? template<typename T> struct NumInfo { T x; T y; void Print(); }; template<typename T> void NumInfo <T>::Print() { /*.....*/ } template<typename T> struct NumInfo <float> { T x; float y; void Print(); }; template<typename T> void NumInfo <float>::Print() { /*.....*/ } 回答1: Your design has a problem -- right now you have multiple classes with the same name NumInfo

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