specialization

C++ basic template question

Hello! I'm slightly confused with template specialization. I have classes Vector2, Vector3 which have operator+= in it (which are defined the following way). Vector2& operator+=(const Vector2& v) { x() += v.x(), y() += v.y(); return *this; } Now I want to add the generic addition behaviour and say something like: ...

C++ Template Specialization Compilation

I'm going to outline my problem in detail to explain what I'm trying to achieve, the question is in the last paragraph if you wish to ignore the details of my problem. I have a problem with a class design in which I wish to pass a value of any type into push() and pop() functions which will convert the value passed into a string represe...

Specialize a generic variable

I have a generic method where I want to do something special for Strings. I've found DirectCast(DirectCast(value, Object), String) to get the String value (when I've already confirmed GetType(T) Is GetType(String)) and DirectCast(DirectCast(newvalue, Object), T) as mentioned in a number of answers to similar questions works. But is the...

template function specialization default argument

template <typename T> void function(T arg1, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { } template <> void function<int>(int arg1, int min,int max) { } int main(int argc,char* argv[]) { function<int>(1); } it give syntax error C2689 and C2059 on function default argument line on :: tok...

C++: Inheritance v. Containment for a templatized class

I have the following struct: template <typename T> struct Odp { T m_t; }; I want to specialize it so I can add an operator so the type plays nicely with STL sets. (I can't modify Odp directly; it's legacy code.) Here are two methods I see of doing it: struct Ftw : public Odp<int> { bool operator==(const Ftw& rhs) { ...

Is it best for multiple specialization or single specialization?

I've noticed a lot about jobs being posted that require the applicant to know several languages or technologies. Especially I find this with web development. I don't really like this considering the point that the more you specialize in several things the less you actually know about each one. I recognize that this is good for the bottom...

gcc with boost for tr1, hash functor specialization

I am using boost for tr1 instead of gcc 4.5's native tr1 libraries (by prioritizing boost tr1 headers over gcc headers). There is a compile problem specializing std::tr1::hash. #include <functional> #include <memory> template <class A> struct std::tr1::hash< std::tr1::shared_ptr<A> > : public unary_function< std::tr1::shared_ptr<A>...

Error with C++ partial specialization of template

I am using PC-Lint (great tool for static code analysis - see http://www.gimpel.com/) For the following chunk of code: class ASD { protected: template<int N> void foo(); }; template<> inline void ASD::foo<1>() {} template<int N> inline void ASD::foo() {} PC-lint gives me a warning: inline void ASD::foo<1>() {} m...

Type specialization; how to determine and react on each specific type?

Imagine that I have a general class Person. Then I have specializations of that class, for example DanishPerson and BritishPerson. Now I need a function that returns the correct instance of Persons, depending on what country they are in, or a way to easily determine what type of persons they are. So I have the function: List<Person> Ge...

Does order matter in specialization of a function in class template

Consider something like... template<typename T> class Vector { ... bool operator==( const Vector<float> &rhs ) { // compare and return } bool operator==( const Vector<T> &rhs ) { // compare and return } ... }; Notice how the specialization is above the non specialized version. If I were to put the specialized ver...

Declaring an instance of an explicit specializtion of a template within a regular class

I can't get this to compile at all. I may not be possible but I don't know why it should not be. class A { template <typename T> class B { int test() { return 0; } }; //template <> class B<int>; <-with this, namepace error B<int> myB_; }; template <> class A::B<int> { int test() { return 1; } }; As it appears ...

C++ single template specialisation with multiple template parameters

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...

Multiple types in one specialized D template

Say I have to deal ushort and uint some way, but string differently. So guess I need one specialized template for string and other to both ushort and uint. Is it? // for most void func(T)(T var) { ... } // for uint and ushort void func(T: uint, ushort)(T var) { ... } That is the idea, although the code can't compile. It's valid or ...

Pattern where only one handler of many should act based on specialization

I'm trying to rewrite some code to break some coupling issues and make it easier to modify in the future. Right now, I have a static factory method in a base class that, depending on the situation, picks an appropriate implementation. The decision is based on degrees of specialization: While types A and B both can handle this, B...

Java generics (template) specialization possible (overriding template types with specific types)

I'm wondering what are the options to specialize generic types in Java, i.e. in a templated class to have specific overrides for certain types. In my case I was a generic class (of type T) to return null usually, but return "" (the empty string), when T is the String type, or 0 (zero) when its the Integer type, etc. Merely providing a ...

C++ template specialization

Hello! Does someone know a way to achieve or emulate the following behaviour? (this code results in compilation-time error). E.g, I want to add specific template specialization only in derived classes. struct Base { template <typename T> void Method(T a) { T b; } template <> void Method<int>(int a) { float c; }...

friend declaration declares a non-template function

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 alread...

Is it possible to overload a template class?

I found that template method could be overloaded, can I do the same on template classes? If 2 template classes match a template class instantiation, we can use the parameter type in the constructor to deduce which one to use. template <typename T> class A{ A(T){} }; template <typename T> class A{ A(T*){} }; int main(){ A<int*> ...