specialization

C++ template specialization of constructor

I have a templated class A<T, int> and two typedefs A<string, 20> and A<string, 30>. How do I override the constructor for A<string, 20> ? The following does not work: template <typename T, int M> class A; typedef A<std::string, 20> one_type; typedef A<std::string, 30> second_type; template <typename T, int M> class A { public: A(...

Specializing a template by a template base class

I'm writing a template for which I'm trying to provide a specialization on a class which itself is a template class. When using it I'm actually instanciating it with derivitives of the templated class, so I have something like this: template<typename T> struct Arg { static inline const size_t Size(const T* arg) { return sizeof(T); }...

Overloading based on specialization in C++

I'm trying to create a function which is overloaded based on the specialization of its parameter, such as this: class DrawableObject...; class Mobile : public DrawableObject...; class Game { AddObject(DrawableObject * object) { // do something with object } AddObject(Mobile * object) { AddObject(dyna...

How does the compiler know to use a template specialization instead of its own instantiation?

Consider the following files: Foo.H template <typename T> struct Foo { int foo(); }; template <typename T> int Foo<T>::foo() { return 6; } Foo.C #include "Foo.H" template <> int Foo<int>::foo() { return 7; } main.C #include <iostream> #include "Foo.H" using namespace std; int main() { Foo<int> f; cout << f.foo() << ...

Help with type traits

Suppose we have the following template class template<typename T> class Wrap { /* ... */ }; We can not change Wrap. It is important. Let there are classes derived from Wrap<T>. For example, class NewInt : public Wrap<int> { /* ... */ }; class MyClass : public Wrap<myclass> { /* ... */ }; class Foo : public Wrap<Bar> { /...

Templates specialization

I have the following set of templates: //1 template< typename T > void funcT( T arg ) { std::cout<<"1: template< typename T > void funcT( T arg )"; } //2 template< typename T > void funcT( T * arg ) { std::cout<<"2: template< typename T > void funcT( T * arg )"; } //3 template<> void funcT< int >( int arg ) ...

Is it possible to specialize on some (not all) class template parameters?

For example: template <typename T, typename U> class TC { public: void Foo(); }; template <typename T, typename U> void TC<T, U>::Foo() { } template <???, typename U> void TC<int, U>::Foo() { //Want this defined for all U but only when T is int. } int main(int argv, char * args []) { TC<int, char> tc; return 0; } Thanks very ...

C# generic interface specialization

I wonder if it is in any way possible to specialize generic interface methods somehow in C#? I have found similar questions, but nothing exactly like this. Now I suspect that the answer is "No, you can't" but I would like to have it confirmed. What I have is something like the following. public interface IStorage { void Store<T>(T ...

static member initialization for specialized template class

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

One template specialization for multiple classes

Let's assume we have a template function "foo": template<class T> void foo(T arg) { ... } I can make specialization for some particular type, e.g. template<> void foo(int arg) { ... } If I wanted to use the same specialization for all builtin numeric types (int, float, double etc.) I would write those lines many times. I know that ...

c++ templates: problem with member specialization

I am attempting to create a template "AutoClass" that create an arbitrary class with an arbitrary set of members, such as: AutoClass<int,int,double,double> a; a.set(1,1); a.set(0,2); a.set(3,99.7); std::cout << "Hello world! " << a.get(0) << " " << a.get(1) << " " << a.get(3) << std::endl; By now I have an AutoClass with a working "se...

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

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

Is there a way to use template specialization to separate new from new[]?

I have an auto pointer class and in the constructor I am passing in a pointer. I want to be able to separate new from new[] in the constructor so that I can properly call delete or delete[] in the destructor. Can this be done through template specialization? I don't want to have to pass in a boolean in the constructor. template <typ...

Does this mimic perfectly a function template specialization?

Since the function template in the following code is a member of a class template, it can't be specialized without specializing the enclosing class. But if the compiler's full optimizations are on (assume Visual Studio 2010), will the if-else-statement in the following code get optimized out? And if it does, wouldn't it mean that for ...

Class Table Inheritance vs. Denormalization

I'm trying to model a specialization/generalization, leaning towards using class table inheritance (see this answer). However, my co-worker has maintenance and performance concerns because there will be many (50+) overlapping specializations of the same table. His suggestion is to create a table with the following columns: Reference t...

Specializing a class template constructor

I'm messing around with template specialization and I ran into a problem with trying to specialize the constructor based on what policy is used. Here is the code I am trying to get to work. #include <cstdlib> #include <ctime> class DiePolicies { public: class RollOnConstruction { }; class CallMethod { }; }; #include <boost/static_as...

std::basic_string full specialization (g++ conflict)

I am trying to define a full specialization of std::basic_string< char, char_traits<char>, allocator<char> > which is typedef'd (in g++) by the <string> header. The problem is, if I include <string> first, g++ sees the typedef as an instantiation of basic_string and gives me errors. If I do my specialization first then I have no issues...

Is there a mean to specify specialization-genralization (inheritance) of actors in UML?

I am just starting to use UML and have came to the following question: Some actors clearly are specialized versions of a natural entity. For example I've got Administrator and User actors which are clearly nothing but different roles of a user, Authorizer and Dispatcher which are services (and are going to be implemented this way). Shou...

Issue with class template partial specialization

I've been trying to implement a function that needs partial template specializations and fallen back to the static struct technique, and I'm having a number of problems. template<typename T> struct PushImpl<const T&> { typedef T* result_type; typedef const T& argument_type; tem...

VS2010 C++ member template function specialization error

I have the following (minimized) code, which worked in VC2005, but no longer works in 2010. template <typename TDataType> class TSpecWrapper { public: typedef typename TDataType::parent_type index_type; public: template <bool THasTriangles> void Spec(index_type& io_index) { std::cout << "False version" << s...