template-specialization

How to specialize a template with template-tempate parameters

▼魔方 西西 提交于 2019-12-07 03:57:28
问题 Edit at the end I have a function which takes a template: template <template <typename ...> class P, typename ... Args> void f(const P<Args...> &p) { std::cout << "Template with " << sizeof...(Args) << " parameters!\n"; } It works pretty good with any kind of templates I've tested so far: f(std::valarray<int>{}); // Prints: "Template with 1 parameters!" f(std::pair<char, char>{}); // Prints: "Template with 2 parameters!" f(std::set<float>{}); // Prints: "Template with 3 parameters!" f(std:

What is a robust way of template specialization in C++ for separated header/source

坚强是说给别人听的谎言 提交于 2019-12-07 03:53:46
问题 In moderate-sized or even big complex projects separating template declaration and definition is useful to reduce compilation time. However, in a complex code small programmer mistakes may lead to unnoticed behaviour change, e.g. a generic version is called instead of a specialization. Example: Template specialization became invisible due to a missed declaration. ///////////////////// file A.hpp ///////////////////// #include <iostream> template <typename T> class A { public: void foo() { std

Template Specialization for basic POD only

落爺英雄遲暮 提交于 2019-12-07 02:35:44
问题 Is there a subtle trick for template specialization so that I can apply one specialization to basic POD (when I say basic POD I don't particularly want struct POD (but I will take that)). template<typename T> struct DoStuff { void operator()() { std::cout << "Generic\n";} }; template<> struct DoStuff</*SOme Magic*/> { void operator()() { std::cout << "POD Type\n";} }; Or do I have to write specializations for each of the built in types? template<typename T> struct DoStuff { void operator()()

Mixing partial template specialization and default template parameters

吃可爱长大的小学妹 提交于 2019-12-07 02:11:51
问题 I would like to create a generic vector class and create specializations for a few cases. Something like this (it does not compile, but hopefully communicates my intentions): template<int dim, typename T = float> class Vector { public: typedef Vector<dim, T> VecType; Vector() { /**/ } Vector(const VecType& other) { /**/ ) Vector& operator=(const VecType& other) { /**/ } VecType operator+(const VecType& other) { /**/ } VecType operator-(const VecType& other) { /**/ } T operator*(const VecType&

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

心不动则不痛 提交于 2019-12-06 19:35:24
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 double& x); // Doing it template <> template <typename T> double MyCLass<T>::myfunct(const double& x) {

C++ template specialization on functions

纵然是瞬间 提交于 2019-12-06 17:49:58
问题 I'm playing around with template specialization, and I've found an issue I can't seem to solve; this is my code: template<int length, typename T> void test(T* array) { ... test<length-1>(array); } template<typename T> void test<0>(T* array) { return; } So what I'm trying to do, is to pass the length, of what's to be processed in the template. The problem is, that the compilation of this, well outputs forever: a.cpp:83:43: error: template-id 'test<0>' in declaration of primary template a.cpp:

Static template member function for template class

旧城冷巷雨未停 提交于 2019-12-06 16:15:07
I have a template class and a template member function: template<class T1> struct A{ template<class T2> static int f(){return 0;} }; I want to specialize for a case when T1 and T2 are the same, For example, define the case A<T>::f<T> for any T . but I can't find the combination of keywords to achieve this. How can I partially (?) specialize a combination of template class and a template static function? These are my unsuccessful attempts, and the error messages: 1) Specialize inside the class: fatal error: cannot specialize a function 'f' within class scope ) template<class T1> struct A{

Specializing member template for enum type arguments

纵饮孤独 提交于 2019-12-06 11:45:14
In the code below, Foo<T>::setValue works well for my purposes, except in cases where where T is a class enum named TYPE e.g. Bar::TYPE and Baz:TYPE . Therefore, I'd appreciate help specializing Foo<T>::setValue without naming Bar and Baz , because there could be dozens of such classes. class Bar { public: enum TYPE{ ONE , TWO }; }; class Baz { public: enum TYPE{ SIX , TEN }; }; template<typename T> class Foo { public: void setValue(){} // Need a different setValue if T is a class enum private: T m_value; }; int main() { Foo<int> f1; Foo<Bar::TYPE> f2; Foo<Baz::TYPE> f3; return EXIT_SUCCESS; }

How to create specialization for a single method in a templated class in C++?

点点圈 提交于 2019-12-06 09:20:45
问题 Many questions have been asked and they are similar to the one I am going to ask here, but they are not the same I think. I have a templated class: namespace app { template <typename T> class MyCLass { public: void dosome(); void doother(); } } /*ns*/ And implementations: template <typename T> app::MyClass<T>::dosome() {} template <typename T> app::MyClass<T>::doother() {} When I have an instance of that class to which a char is provided as template parameter, I want function dosome() to

Partial specilization of static variable template in class template

你说的曾经没有我的故事 提交于 2019-12-06 05:27:28
问题 If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: template <typename T> static X<T> x_in_y; }; template< typename T2> template< typename T> X<T> Y<T2>::x_in_y{200}; template<> template<> X<float> Y<int>::x_in_y<float>{100}; template<> template<> X<int> Y<int>::x_in_y<int>{101}; template< > template< typename T > X<T> Y<bool>::x_in_y{77