template-specialization

enable_if method specialization

狂风中的少年 提交于 2019-11-30 03:54:09
template<typename T> struct A { A<T> operator%( const T& x); }; template<typename T> A<T> A<T>::operator%( const T& x ) { ... } How can I use enable_if to make the following specialization happen for any floating point type (is_floating_point)? template<> A<float> A<float>::operator%( const float& x ) { ... } EDIT: Here's an answer I came up which is different from the ones posted below... template<typename T> struct A { T x; A( const T& _x ) : x(_x) {} template<typename Q> typename std::enable_if<std::is_same<Q, T>::value && std::is_floating_point<Q>::value, A<T> >::type operator% ( const Q&

Template specialization with variadic templates

半腔热情 提交于 2019-11-30 00:35:34
问题 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? 回答1:

C++ inconsistency between gcc and clang

别说谁变了你拦得住时间么 提交于 2019-11-29 16:02:42
问题 I came across a C++ inconsistency between gcc (versions 4.8.1 , 4.8.2 ) and clang (versions 3.3 , 3.4 ). I wonder which one is correct. Here's the program: template < typename T > struct Result {}; template < typename T > struct Empty {}; template < typename T > struct Bad_Type_Fcn { typedef typename Empty< T >::type type; }; template < typename T > Result< T > f( const T& ) { return Result< T >(); } template< class U > Result< typename Bad_Type_Fcn< U >::type > f( const U&, int ) { return

partial specialization of function templates

六月ゝ 毕业季﹏ 提交于 2019-11-29 14:02:30
问题 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

Specializing function template for reference types

不羁岁月 提交于 2019-11-29 09:26:55
Why is the output of this code : #include <iostream> template<typename T> void f(T param) { std::cout << "General" << std::endl ; } template<> void f(int& param) { std::cout << "int&" << std::endl ; } int main() { float x ; f (x) ; int y ; f (y) ; int& z = y ; f (z) ; } is General General General The third one is surprizing because the function was specialized exactly for int& Edit : I know that overloading might be a proper solution. I just want to learn the logic behind it. The type of both the expression y and the expression z is int . A reference appearing in an expression won't keep

c++ template specialization for all subclasses

ⅰ亾dé卋堺 提交于 2019-11-29 09:03:30
问题 I need to create a template function like this: template<typename T> void foo(T a) { if (T is a subclass of class Bar) do this else do something else } I can also imagine doing it using template specialization ... but I have never seen a template specialization for all subclasses of a superclass. I don't want to repeat specialization code for each subclass 回答1: You can do what you want but not how you are trying to do it! You can use std::enable_if together with std::is_base_of : #include

Template specialization for enum

China☆狼群 提交于 2019-11-29 06:52:14
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? James McNellis You can use std::enable_if with std::is_enum from <type_traits> to accomplish this. In an answer to one of my questions , litb posted a very detailed and well-written explanation of how this

Can variadic template template parameter be partial-specialized?

我的未来我决定 提交于 2019-11-29 06:17:30
问题 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

Specializing C++ template based on presence/absense of a class member?

ぃ、小莉子 提交于 2019-11-29 04:17:47
Consider the following: struct A { typedef int foo; }; struct B {}; template<class T, bool has_foo = /* ??? */> struct C {}; I want to specialize C so that C<A> gets one specialization and C<B> gets the other, based on the presence or absence of typename T::foo. Is this possible using type traits or some other template magic? The problem is that everything I've tried produces a compile error when instantiating C<B> because B::foo doesn't exist. But that's what I want to test! Edit: I think ildjarn's answer is better, but I finally came up with the following C++11 solution. Man is it hacky, but

Template class member specialization without declaration in header

假如想象 提交于 2019-11-29 03:47:18
I have a template class that I declare in a header with one method and no definition of that method in the header. In a .cc file, I define specializations of that method without ever declaring them in the header . In a different .cc file, I call the method for different template parameters for which specializations exist. It looks like this: foo.h: template<typename T> class Foo { public: static int bar(); }; foo.cc: #include "foo.h" template<> int Foo<int>::bar() { return 1; } template<> int Foo<double>::bar() { return 2; } main.cc: #include <iostream> #include "foo.h" int main(int argc, char