specialization

C++ templated container class: How to best support both ordered and un-ordered item types?

Hi all, I'm writing a templated C++ generic container class that can optionally maintain its contents in a well-defined order. Previously it used function pointers to order its contents in a sensible type-specific way, but I am attempting to change it to use templated functor arguments instead. Since it's often the case that the clas...

Function template specialization with reference to pointer.

I have a template function: template<typename T> void foo(const T& value) { bar(value); x = -1; } I want to specialize it for a set of types: template<> void foo<char>(const char& value) { bar(value); x = 0; } template<> void foo<unsigned char>(const unsigned char& value) { bar(value); x = 1; } It works ok. When I compile this: te...

template specialization of template class

Hello, I want to specialize following member function: class foo { template<typename T> T get() const; }; To other class bar that depends on templates as well. For example, I would like bar to be std::pair with some template parameters, something like that: template<> std::pair<T1,T2> foo::get() const { T1 x=...; T2...

Templated class function T: How to find out if T is a pointer?

As a follow-up to this question: I need to decide in a class function like this: template< typename T > bool Class::Fun <T*> ( T& variable ) {...} whether T is a pointer or not. In the question cited above the answer was to use partial template specialization. As far as I've found out this is not possible for class functions. Is this...

Template specialization error - C++ (C++ Primer Plus exercise)

Hi! I'm currently learning C++ so I don't have much knowledge on the topic . I'm using the C++ primer plus book and here's the problem : Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array....

Template specialization of particular members?

Is it possible to specialize particular members of a template class? Something like: template <typename T,bool B> struct X { void Specialized(); }; template <typename T> void X<T,true>::Specialized() { ... } template <typename T> void X<T,false>::Specialized() { ... } Ofcourse, this code isn't valid. ...

C++ specialization, type_of or just typeid

Hello, I would like to know what is better to use in my situation and why. First of all I heard that using RTTI (typeid) is bad. Anyone could explain why? If I know exactly types what is wrong to compare them in a runtime? Furthermore is there any example how to use boost::type_of? I have found none searching through the mighty google :...

Specialization vs. Diversification

I have read a few of the questions asked on this topic, here on StackOverflow, but most of them focus on different areas within Computer Science. I am currently working in a start up unit in a large financial company. Our whole team just finished our MSc. or PhD. in our various fields, however, none of us have a lot of real world experi...

C++ template specialization without default function

Hello, I have the following code that compiles and works well: template<typename T> T GetGlobal(const char *name); template<> int GetGlobal<int>(const char *name); template<> double GetGlobal<double>(const char *name); However I want to remove the "default" function. That is, I want to make all calls to GetGlobal<t> where 't' is not...

C++ template specialization with <int&> not picking up an int

Hi, I have the following code: template <typename T> LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0); } template <> LuaCall& operator>><int&>(int& val) { mResults.push_back(std::make_pair(LUA_RESULT_INTEGER, (void *)&val)); return *this; } template <> LuaCall& operator>><float&>(float& val) { mResults.push_back(std::make_pa...

C++ template specialization via a base class

Dear Overflowers, I want to be able to make the compiler shout when i call a constructor of foo with a class that is NOT derived from base. The current code allows only for foo<base> itself. Any easy solution ? class _base { public: // ... }; class _derived: public _base { public: // ... }; template <typename T> class foo { ...

Using valid STATIC member function of class that can't be installed

Hello, I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel compiler, but fails under MSVC9. MSVC tells "use of undefined type c_traits<C>, while compiling class template member function void foo<C>::go(void) with C=short. The point it the compiler tries to install unused member function of unuse...

Template specialization problem

Hi, I'm trying really hard to made this work, but I'm having no luck. I'm sure there is a work around, but I haven't run across it yet. Alright, let's see if I can describe the problem and the needs simply enough: I have a RGB template class that can take typenames as one of its template parameters. It takes the typename and sends it...

Need a vector that derives from a vector.

Hi, Consider this simple code: class A { }; class V1: vector<A *>{ // my nice functions }; if I have a instance of V1, then any object derived from A can be inserted into the vector, ok here. Now, lets say I have two simple classes called B and C both derives from A; if I have a instance of V1, then both pointers of B and C can...

Template specialization of a single method from a templated class

Hello. Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly: template <class T> class TClass { public: void doSomething(std::vector<T> * v); }; template <class T> void TClass<T>::doSomething(std::vector<T> * v) { // Do somtehing with a vect...

Template specialization with a templatized type

I want to specialize a class template with the following function: template <typename T> class Foo { public: static int bar(); }; The function has no arguments and shall return a result based on the type of Foo. (In this toy example, we return the number of bytes of the type, but in the actual application we want to return some me...

Problem with class template specialisations

I'm trying to port some code from VC9 to G++, however Ive run into a problem with template specialisations apparently not being allowed for class members. The following code is an example of these errors for the getValue specialisations of the class methods. In all cases the error is "error: explicit specialization in non-namespace scop...

Specialization of template after instantiation?

Hi, My full code is too long, but here is a snippet that will reflect the essence of my problem: class BPCFGParser { public: ... ... class Edge { ... ... }; class ActiveEquivClass { ... ... }; class PassiveEquivClass { ... ... }; struct EqActiveEquivClass { ... ... }; struc...

specialization on const member function pointers

I am trying to specialize some utility code on const member functions, but have problems to get a simple test-case to work. To simplify the work i am utilizing Boost.FunctionTypes and its components<FunctionType> template - a MPL sequence which should contain the tag const_qualified for const member functions. But using the test-code be...

Overriding multiple inherited templated functions with specialized versions

Okay, sample code first; this is my attempt at communicating what it is that I'm trying to do, although it doesn't compile: #include <iostream> template <class T> class Base { public: virtual void my_callback() = 0; }; class Derived1 : public Base<int> , public Base<float> { public: void my_callback<int>() { cout << "Int callba...