specialization

Template specialization of a single method from templated class with multiple template parameters

非 Y 不嫁゛ 提交于 2019-11-28 08:54:45
问题 I'm basically trying to do what was discussed in Template specialization of a single method from a templated class except that my TClass has multiple template Parameters like this: template < class KEY, class TYPE > class TClass { public: : void doSomething(KEY * v); : }; template < class KEY, class TYPE > void TClass<KEY, TYPE>::doSomething(KEY * v) { // do something } This works so far, but how do I define a specialized implementation for one template Parameter? I tried adding this:

C++ single template specialisation with multiple template parameters

空扰寡人 提交于 2019-11-28 08:40:42
Hallo! I would like to specialise only one of two template types. E.g. template <typename A, typename B> class X should have a special implementation for a single function X<float, sometype>::someFunc() . Sample code: main.h: #include <iostream> template <typename F, typename I> class B { public: void someFunc() { std::cout << "normal" << std::endl; }; void someFuncNotSpecial() { std::cout << "normal" << std::endl; }; }; template <typename I> void B<float, I>::someFunc(); main.cpp: #include <iostream> #include "main.h" using namespace std; template <typename I> void B<float, I>::someFunc() {

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

ぃ、小莉子 提交于 2019-11-28 07:27:47
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 would be legal anyway.) You can use enable_if with the is_unsigned type trait: template <typename T>

Specializing function template for reference types

﹥>﹥吖頭↗ 提交于 2019-11-28 02:51:11
问题 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. 回答1: The type of

template class member function only specialization

风格不统一 提交于 2019-11-27 19:41:14
I am reading the Complete Guide on Templates and it says the following: Where it is talking about class template specialization. However, if you specialize a class template, you must also specialize all member functions. Although it is possible to specialize a single member function, once you have done so, you can no longer specialize the whole class. I'm actually wondering how this is true, cause you can specialize without any member functions at all. Is it saying that you cannot have a specialization with only one member function and then another with all member functions? Can someone please

Default template parameter partial specialization

限于喜欢 提交于 2019-11-27 19:14:58
Please explain to me why the following piece of code complies and works perfectly. I am very confused. #include<iostream> template<class A = int, class B=double> class Base {}; template<class B> class Base <int, B> { public: Base() { std::cout<<"it works!!!!!\n"; } }; int main() { Base<> base; // it prints "it works!!!!!" return 0; } Shouldn't it fall into the generalized form of the template class Base? The default argument applies to the specialization -- and, in fact, a specialization must accept (so to speak) the base template's default argument(s). Attempting to specify a default in the

Specializing a template on a lambda in C++0x

半城伤御伤魂 提交于 2019-11-27 18:33:53
I've written a traits class that lets me extract information about the arguments and type of a function or function object in C++0x (tested with gcc 4.5.0). The general case handles function objects: template <typename F> struct function_traits { template <typename R, typename... A> struct _internal { }; template <typename R, typename... A> struct _internal<R (F::*)(A...)> { // ... }; typedef typename _internal<decltype(&F::operator())>::<<nested types go here>>; }; Then I have a specialization for plain functions at global scope: template <typename R, typename... A> struct function_traits<R (

c++ class template specialization, without having to reimplement everything

青春壹個敷衍的年華 提交于 2019-11-27 14:26:33
问题 I have a templatized class like so : template<typename T> class A { protected: std::vector<T> myVector; public: /* constructors + a bunch of member functions here */ } I would like to add just ONE member function that would work only for 1 given type of T. Is it possible to do that at all without having to specialize the class and reimplement all the other already existing methods? Thanks 回答1: The simplest and cleanest solution is to use a static_assert() in the body of a method, rejecting

static member initialization for specialized template class

痞子三分冷 提交于 2019-11-27 14:01:54
class A { }; template <typename A, int S> class B { public: static int a[S]; B() { a[0] = 0; } }; template<> int B<A, 1>::a[1]; int main() { B<A, 1> t; t; } It compiles under GCC 4.1, but does not link: static.cpp:(.text._ZN1BI1ALi1EEC1Ev[B<A, 1>::B()]+0x5): undefined reference to `B<A, 1>::a' I would prefer to keep initialisation specialised if it is possible, since the array holds some data specific to the type. For static member specializations, if you don't initialize the member, it is taken as a specialization declaration , that just says "Oh, don't instantiate the member from the primary

friend declaration declares a non-template function [duplicate]

折月煮酒 提交于 2019-11-27 11:46:23
This question already has an answer here: overloading friend operator<< for template class 5 answers I have a base Class akin to the code below. I'm attempting to overload << to use with cout. However, g++ is saying: base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning I've tried adding <> after << in the class declaration /