template-specialization

Default values of template parameters in the class template specializations

我的未来我决定 提交于 2019-12-06 03:36:39
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate { }; template <class x1, class x2> struct CoreTemplate<x1*, x2*> { int spec; CoreTemplate() { spec = 1; } }; template <class x> struct CoreTemplate<x*> { int spec; CoreTemplate() { spec = 3; } }; int main(int argc, char* argv[]) { CoreTemplate<int*, int*> qq1; printf("var=%d.\r\n", qq1.spec); CoreTemplate<int*> qq2; printf("var=%d.\r\n", qq2.spec); } MSVC compiles this code fine and selects the second specialization in both cases. For me these specializations are identical. How legal is the second

c++ How to initialize static variables of a partial template specialization

喜你入骨 提交于 2019-12-06 01:31:13
问题 How should I initialize a static variable for a partial specialization? template <bool A=true, bool B=false> struct from { const static std::string value; }; // no specialization - works template <bool A, bool B> const std::string from<A, B>::value = ""; // partial specialization - does not compile - // Error: template argument list following class template name must list parameters in the order used in template parameter list // Error: from<A,B>' : too few template arguments template <bool B

Adding specializations of template functions later

南笙酒味 提交于 2019-12-06 01:01:11
Suppose I have functions like: template<typename T> inline typename std::enable_if<has_member_foo<T>::value,int>::type foo( T const &t ) { return t.foo(); } template<typename T> inline typename std::enable_if<!has_member_foo<T>::value,int>::type foo( T const& ) { return 0; } template<typename T> inline int call_foo( T const &t ) { return sizeof( T ) + foo( t ); } This mostly works fine, but if I later add an overload for a particular type: inline int foo( std::string const &s ) { return s.size(); } and I add it after the definition of call_foo() , the overload isn't used by call_foo() .

Is it legal to perform partial in-class specialization of a member template class in derived class

大城市里の小女人 提交于 2019-12-05 23:01:20
问题 It is continuation of this question. I am specifically interested if the partial specialization of a member class like this: struct FooParent { template <class> struct Bar{ }; }; struct Foo: FooParent { template <class T> struct Bar<T*> {}; }; I know this can be done inside a namespace scope: template <class T> struct Foo::Bar<T*>{ }; But I'm also specifically interested in in-class partial specialization at the level of derived class. Both clang and gcc complains when encounter a former:

Template struct with the default template argument is not instantiated

纵饮孤独 提交于 2019-12-05 18:43:02
Let's say I have this code template<typename T2, typename T = int> struct X { static double f; }; template<typename T> double X<T>::f = 14.0; If I try to compile clang give me the following error nested name specifier 'X::' for declaration does not refer into a class, class template or class template partial specialization and for GCC : error: template definition of non-template 'double X::f' The question is : Why the compiler want us to specialize the struct X like that : template<typename T2> struct X<T2,int> { static double f; }; The first declaration has int as a default argument, why the

Partial Template Specialization restricted to certain types

浪子不回头ぞ 提交于 2019-12-05 11:44:45
is it possible to write a partial template specialization that is used only for class types that, for example, inherit from a specific class or comply with some other constraint that can be expressed via type traits? i.e., something like this: class A{} class B : public A{} template<typename T> class X{ int foo(){ return 4; } }; //Insert some magic that allows this partial specialization //only for classes which are a subtype of A template<typename T> class X<T>{ int foo(){ return 5; } }; int main(){ X<int> x; x.foo(); //Returns 4 X<A> y; y.foo(); //Returns 5 X<B> z; z.foo(); //Returns 5 X<A*>

Specializing a variadic template template parameter on the minimum number of arguments: legal or not?

半世苍凉 提交于 2019-12-05 11:25:36
问题 I have code: #include <cstdio> template<template<typename...> class> struct Foo { enum { n = 77 }; }; template<template<typename, typename...> class C> struct Foo<C> { enum { n = 99 }; }; template<typename...> struct A { }; template<typename, typename...> struct B { }; int main(int, char**) { printf("%d\n", Foo<A>::n); printf("%d\n", Foo<B>::n); } The idea is that template<typename, typename...> class is a subset of template<typename...> class , so it might be possible to specialize on it.

C++14 warning: too many template headers for variable (should be 0)

狂风中的少年 提交于 2019-12-05 10:15:12
问题 While experimenting with the recent g++-5 compiler, I wrote below statement in a file: template<T> T a; template<> int a = 1; Which results in: warning: too many template headers for a (should be 0) Also effectively, it doesn't really specialize a<int> . e.g. template<typename T> T a; template<> int a = 1; int main () { std::cout << a<double> << "\n"; // prints 0; OK std::cout << a<int> << "\n"; // prints 0! why not 1? } What is the mystery about this syntax? 回答1: Template arguments can only

Template Specialization for basic POD only

旧街凉风 提交于 2019-12-05 08:01:01
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()() { std::cout << "Generic\n";} }; // Repeat the following template for each of // unsigned long long,

Mixing partial template specialization and default template parameters

孤人 提交于 2019-12-05 06:02:03
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& other) { /**/ } private: std::array<T, dim> elements; }; template<int dim, typename T> class Vector<2>