specialization

Partial specialization of double-templated method fails

你说的曾经没有我的故事 提交于 2019-12-18 16:33:09
问题 There is the template class List. template <typename Point> class List { public: template <const unsigned short N> void load ( const char *file); ... }; template <typename Point> template <const unsigned short N> void List <Point>::load ( const char *file) } How to specialize method load for N=2? This code is not valid... template <typename Point> void List <Point> <2>::load ( const char *file) { } And this code also does not work. template <typename Point> void List <Point> ::load <2> (

Is specialization of std::to_string for custom types allowed by the C++ standard?

坚强是说给别人听的谎言 提交于 2019-12-18 13:53:10
问题 In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? namespace std { string to_string(::MyClass const & c) { return c.toString(); } } Sample use-case: int main() { MyClass c; std::cout << std::to_string(c) << std::endl; } 回答1: In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? No. First of all, it is not a template function so you can't specialize it at all. If you're asking about adding your

How can I get a specialized template to use the unspecialized version of a member function?

会有一股神秘感。 提交于 2019-12-18 13:37:56
问题 Consider the following code: template <int dim> struct vec { vec normalize(); }; template <> struct vec<3> { vec cross_product(const vec& second); vec normalize(); }; template <int dim> vec<dim> vec<dim>::normalize() { // code to normalize vector here return *this; } int main() { vec<3> direction; direction.normalize(); } Compiling this code produces the following error: 1>main.obj : error LNK2019: unresolved external symbol "public: struct vec<3> __thiscall vec<3>::normalize(void)" (

Partial template specialization based on “signed-ness” of integer type?

折月煮酒 提交于 2019-12-17 18:26:28
问题 Given: template<typename T> inline bool f( T n ) { return n >= 0 && n <= 100; } When used with an unsigned type generates a warning: unsigned n; f( n ); // warning: comparison n >= 0 is always true Is there any clever way not to do the comparison n >= 0 when T is an unsigned type? I tried adding a partial template specialization: template<typename T> inline bool f( unsigned T n ) { return n <= 100; } but gcc 4.2.1 doesn't like that. (I didn't think that kind of partial template specialization

Undefined reference to partial specialized template class function during link time

早过忘川 提交于 2019-12-13 03:59:01
问题 So I had an problem with partial specialization of function templates. I choose the solution described here: Question Now I have this: #include <vector> #include <iostream> template <typename T> struct helper { static void print(T value) { std::cout << value; } }; template <typename T> struct helper<std::vector<T>> { static void print(std::vector<T> const &value) { } }; template <typename T> void print (T const &value) { // Just delegate. helper<T>::print (value); } int main () { print (5);

C++ Template specialization linker error

纵饮孤独 提交于 2019-12-13 02:36:37
问题 How can I separate header file from cpp file when I have template specialization ? I have seen some posts on how to separate header from implementation for template by including cpp file at the end of header file. But this strategy does not work when I have to have template specialization. here is my header file #ifndef H_SUM_H #define H_SUM_H #include <map> #include <string> template <typename T> double Sum(const T &source); template <> double Sum(const std::map<std::string, double> &source)

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

How to specialize template member functions in template class (already specilzied)?

孤者浪人 提交于 2019-12-12 18:22:35
问题 For example: template<unsigned number> struct A { template<class T> static void Fun() {} }; template<> struct A<1> { template<class T> static void Fun() { /* some code here. */ } }; And want to specialize A<1>::Fun() template<> template<> void A<1>::Fun<int>() { /* some code here. */ } doesn't seem to work. How to do it? Thanks. 回答1: An explicit specialization of a class template is like a regular class (it is fully instantiated, so it is not a parametric type). Therefore, you do not need the

Partial specialization with type nested in a templated class

雨燕双飞 提交于 2019-12-12 08:48:53
问题 I'm playing with templates and partial specialization, but there is one specialization I don't know how to write... I'll simplify code to make it easier to read. Let's condiser template <typename T> class x { ... }; Usually, I can specialize like this : class x<a_type> { ... }; Also works with templates types : template <typename T> class x<std::vector<T>> { ... } Now I would like to make the specialization for a type nested in a templated class: template <typename T> class y { struct nested

C++ linking and template specializations

大城市里の小女人 提交于 2019-12-12 08:22:22
问题 I'm studying the behavior of the C++ linker with respect to template specializations. I'm using Microsoft Visual C++ 2010 for these experiments. I don't know if the behavior is the same with other toolchains (e.g. gcc). Here's a first code snippet: // bar.cpp template <typename T> int foo() { return 1; } int bar() { return foo<double>(); } // main.cpp template <typename T> int foo() { return 1; } template <> int foo<double>() { return 2; } int bar(); int main() { const int x = bar(); const