template-specialization

Derived template override return type of member function C++

I am writing matrix classes. Take a look at this definition: template <typename T, unsigned int dimension_x, unsigned int dimension_y> class generic_matrix { ... generic_matrix<T, dimension_x - 1, dimension_y - 1> minor(unsigned int x, unsigned int y) const { ... } ... } template <typename T, unsigned int dimension> class ...

Link error for user defined class type template parameter

Hi, I implemented a Simple STL map in C++. Factored out the comparison as a type as I was instructed to, then implemented the comparison as shown below: template <typename T> int KeyCompare<T>::operator () (T tKey1, T tKey2) { if(tKey1 < tKey2) return -1; else if(tKey1 > tKey2) return 1; else r...

Smplest way to provide template specialization for derived classes

I have following scenario: class my_base { ... } class my_derived : public my_base { ... }; template<typename X> struct my_traits. I want to specialize my_traits all classes derived from my_base including: i.e. template<typname Y> // Y is derived form my_base. stryct my_traits { ... }; I have no problems to add any tags, members...

Providing *implicit* conversion operator for template specialization

I have a templated sparse_vector<T> class, and I am also using Boost UBLAS. How would I provide implicit conversions between sparse_vector<double> and boost::numeric::ublas::compressed_vector<double>? I would also like to provide similar conversions between std::vector<double> and boost::numeric::ublas::vector<double>. (I am using gcc...

Do I need multiple template specializations if I want to specialize for several kinds of strings?

For example: template<typename T> void write(T value) { mystream << value; } template<> void write<const char*>(const char* value) { write_escaped(mystream, value); } template<> void write<char*>(char* value) { write_escaped(mystream, value); } template<> void write<const std::string&>(const std::string& value) { writ...

Using template parameters as template parameters

Why is the following code invalid? template <typename S, typename T> struct B{ void f(T t, S s) {t.f<S>(s); } }; gcc 4.3.4 complains that it "expected primary-expression before '>' token", i.e. that "S" wasn't a valid primary-expression. ...

C++ template specialization

I have a class template <typename T> class C { static const int K=1; static ostream& print(ostream& os, const T& t) { return os << t;} }; I would like to specialize C for int. //specialization for int template <> C<int>{ static const int K=2; } I want the default print method that works for int to remain...

C++ template function specialization using TCHAR on Visual Studio 2005

I'm writing a logging class that uses a templatized operator<< function. I'm specializing the template function on wide-character string so that I can do some wide-to-narrow translation before writing the log message. I can't get TCHAR to work properly - it doesn't use the specialization. Ideas? Here's the pertinent code: // Log.h head...

Specializing a template member function of a template class?

I have a template class that has a template member function that needs to be specialized, as in: template <typename T> class X { public: template <typename U> void Y() {} template <> void Y<int>() {} }; Altough VC handles this correctly, apperantly this isn't standard and GCC complains: explicit specialization in non-...

C++ compiler error on template specialization

I would like to specialize a template method for a class C that is itself templated by an int parameter. How do I do this? template <int D=1> class C { static std::string foo () { stringstream ss; ss << D << endl; return ss.str();} }; template <class X> void test() { cout << "This is a test" << endl;} template <> template <...

Why aren't template specializations allowed to be in different namespaces?

Hi, Please, see what I am trying to do: #include <iostream> namespace first { template <class T> class myclass { T t; public: void who_are_you() const { std::cout << "first::myclass"; } }; } namespace second { using first::myclass; template <> class myclass <int> { int i, j; public: void who_are_you() const { std...

how to specialize templated member functions of non-templated classes?

suppose I have a file alpha.h: class Alpha { public: template<typename T> void foo(); }; template<> void Alpha::foo<int>() {} template<> void Alpha::foo<float>() {} If I include alpha.h in more than one cpp file and compile with GCC 4.4, it complains there are multiple definitions of foo<int> and foo<float> across multiple object...

Typedefs and template specialization

Consider this code: typedef int type1; typedef int type2; template <typename> struct some_trait; template <> struct some_trait<type1> { static const int something=1; }; template <> struct some_trait<type2> { static const int something=2; }; It fails because what the compiler sees is two specializations of some_trait<int>. ...

"unresolved external symbol" on template specialization for array of char

I have something like this in my code: template <typename T> struct A { void Print(); }; template <> struct A<char*> { void Print() { printf("Char*!\n"); } }; template <typename T> void DoSomething(T& lol) { A<T> a; a.Print(); } int main() { char a[5]; DoSomething(a); } And this produces the following linker error: err...

SFINAE: some failures more equal than others?

I'm trying to use SFINAE to distinguish a class that has a member called 'name'. I set things up in what seems to be the standard pattern but it's not working -- instead of silently ignoring the 'failed' substitution, the compiler produces an error. I'm sure I've run up against some template substitution rule, I'd be grateful if someon...

template specialization inside class namespace

How to specialize a template defined in some external namespace in the body of my class? Concrete example using BGL which doesn't compile: class A { namespace boost { template <class ValueType> struct container_gen<SomeSelectorS, ValueType> { typedef std::multiset<ValueType,MyClass<ValueType> > type; }; } } ...

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

How to create a type-tag for template specialization

I have a custom logging class that supports iostream-syntax via a templated operator <<: template< class T > MyLoggingClass & operator <<(MyLoggingClass &, const T &) { // do stuff } I also have a specialized version of this operator that is supposed to be called when a log-message is complete: template< > MyLoggingClass & operat...

Template specialization: non-inline function definition issue.

The following code compiles properly. #include <string> template <typename T, typename U> class Container { private: T value1; U value2; public: Container(){} void doSomething(T val1, U val2); }; template<typename T, typename U> void Container<typename T, typename U>::doSomething(T val1, U val2) { ; // Some impleme...

Why explicit instantiation of outer class template is required before explicit instantiation of a class template

My question is w.r.t the following thread : http://stackoverflow.com/questions/2009924/specialize-a-member-template-without-specializing-its-parent I'm absolutely fine with the standard saying that it is illegal to do so. But i want to understand why is it illegal to do so? What would be impact had it been allowed? ...