template-specialization

How to simulate a partial specialization of selected member functions based on a template parameter that is an STL container?

拈花ヽ惹草 提交于 2019-12-01 01:28:04
问题 I am working with a class that uses STL containers as a template parameter. Not all containers provide the same methods though, so I am trying to figure out how I can specialise specific methods based on the container used. Example: template<typename container> class A { private: container m_container; public: void foo(); // does something container specific // more generic methods that work with any container }; The following code doexsn't compile saying "Can't match method specialisation",

Template specialization with variadic templates

本秂侑毒 提交于 2019-11-30 17:12:14
template <size_t size, typename ...Params> void doStuff(Params...) { } template <> void doStuff<size_t(1), int, bool>(int, bool) { } int main(int, char**) { doStuff<1,int,bool>(1, false); return 0; } This doesn't compile, the second doStuff declaration gives me error: template-id ‘doStuff<1u, int, bool>’ for ‘void doStuff(int, bool)’ does not match any template declaration but it clearly matches the first declaration with variadic template arguments. How to specialize variadic templates? The syntax is correct (afaik, and clang++ accepts it), but your compiler is probably just not up2date yet.

Is sizeof… allowed in template arguments for specialization?

余生颓废 提交于 2019-11-30 15:37:24
I'm trying to do something along the lines of this using GCC 4.7 snapshot: template <int n, int... xs> struct foo { static const int value = 0; }; // partial specialization where n is number of ints in xs: template <int... xs> struct foo<sizeof...(xs), xs...> { // error: template argument ‘sizeof (xs ...)’ // involves template parameter(s) static const int value = 1; }; template <int... xs> struct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int // even though packs aren't expanded static const int value = 2; }; The error is strange because sizeof instead of sizeof...

Explicit specialization after instantiation

醉酒当歌 提交于 2019-11-30 14:52:54
问题 I have the following code: typedef vector<int> Vec; typedef vector<Vec> VecOfVec; template<typename Vec> Vec DoSomething(const Vec &v); template<> VecOfVec DoSomething<VecOfVec>(const VecOfVec &v) { VecOfVec r; for(auto i = v.begin(); i != v.end(); i++) r.push_back(DoSomething(*i)); return r; } template<> Vec DoSomething<Vec>(const Vec &v) // Error here { return v; // for the sake of the example } I get the following error: explicit specialization of 'DoSomething<vector<int> >' after

Disambiguate template specialization between map-like and vector-like containers

陌路散爱 提交于 2019-11-30 14:40:01
问题 template<class> struct Printer; // I want this to match std::vector (and similar linear containers) template<template<class, class...> class T, class TV, class... TS> struct Printer<T<TV, TS...>> { ... }; // I want this to match std::map (and similar map-like containers) template<template<class, class, class...> class TM, class TK, class TV, typename... TS> struct Printer<TM<TK, TV, TS...>> { ... } int main() { // Both of these match the second specialization, which is only intended // for

Disambiguate template specialization between map-like and vector-like containers

时光怂恿深爱的人放手 提交于 2019-11-30 11:17:35
template<class> struct Printer; // I want this to match std::vector (and similar linear containers) template<template<class, class...> class T, class TV, class... TS> struct Printer<T<TV, TS...>> { ... }; // I want this to match std::map (and similar map-like containers) template<template<class, class, class...> class TM, class TK, class TV, typename... TS> struct Printer<TM<TK, TV, TS...>> { ... } int main() { // Both of these match the second specialization, which is only intended // for std::map (and similar map-like containers) Printer<std::vector<int>>::something(); Printer<std::map<int,

partial specialization of function templates

只愿长相守 提交于 2019-11-30 09:44:33
In the below code snippet, template<typename T1> void func(T1& t) { cout << "all" << endl; } template<typename T2> void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int i; float f; func(i); // should print "all" func(f); // should print "float" return 0; } I would like to have the templates modified which by passing any type other than float will print "all" and passing float will print "float". I do not want template specialization, instead have partial specialization which will act accordingly based on input type. How should i

Template specialization for enum

梦想的初衷 提交于 2019-11-30 08:32:20
问题 Is it possible to specialize a templatized method for enums? Something like (the invalid code below): template <typename T> void f(T value); template <> void f<enum T>(T value); In the case it's not possible, then supposing I have specializations for a number of types, like int , unsigned int , long long , unsigned long long , etc, then which of the specializations an enum value will use? 回答1: You can use std::enable_if with std::is_enum from <type_traits> to accomplish this. In an answer to

Can variadic template template parameter be partial-specialized?

℡╲_俬逩灬. 提交于 2019-11-30 06:39:41
Consider the following program: template<template<typename ...> class> struct foo {}; template<template<typename> class C> struct foo<C> {}; int main() {} Clang rejects it with error: class template partial specialization does not specialize any template argument even in latest clang 7.0 HEAD, see demo here . However, gcc accepts it . Refer to [temp.class.spec] where the rules of partial specialization are stated, I couldn't find anything that prohibits the partial specialization of this template. Especially, the specialization is indeed more specialized , the error message looks incorrect.

How to specialize only some members of a template class?

﹥>﹥吖頭↗ 提交于 2019-11-30 05:10:09
Code: template<class T> struct A { void f1() {}; void f2() {}; }; template<> struct A<int> { void f2() {}; }; int main() { A<int> data; data.f1(); data.f2(); }; ERROR: test.cpp: In function 'int main()': test.cpp:16: error: 'struct A<int>' has no member named 'f1' Basically, I only want to specialize one function and use the common definition for other functions. (In actual code, I have many functions which I don't want to specialize). How to do this? Thanks! Consider moving common parts to a base class: template <typename T> struct ABase { void f1(); }; template <typename T> struct A : ABase