template-specialization

Do template specializations require template<> syntax?

I have a visitor class resembling this: struct Visitor { template <typename T> void operator()(T t) { ... } void operator()(bool b) { ... } }; Clearly, operator()(bool b) is intended to be a specialization of the preceding template function. However, it doesn't have the template<> syntax ...

Function template specialization format

What is the reason for the second brackets <> in the following function template: template<> void doh::operator()<>(int i) This came up in SO question where it was suggested that there are brackets missing after operator(), however I could not find the explanation. I understand the meaning if it was a type specialization (full specia...

Is it possible to access values of non-type template parameters in specialized template class?

Is it possible to access values of non-type template parameters in specialized template class? If I have template class with specialization: template <int major, int minor> struct A { void f() { cout << major << endl; } } template <> struct A<4,0> { void f() { cout << ??? << endl; } } I know it the above ca...

C++ Template Specialization

The following template specialization code template<typename T1, typename T2> void spec1() { } Test case 1 template< typename T1> //compile error void spec1<int>() { } Test case 2 template< typename T2> //compile error void spec1<int>() { } generates a error C2768: 'spec1' : illegal use of explicit template arguments compil...

Creating a new primitive type

Is there a way to create a new type that is like one of the basic types (eg char), and can be implcitly converted between, but will resolve diffrently in templates, such that for example, the following code works? typedef char utf8; template<typename T>void f(T c); template<> void f<char>(char c) { std::cout << "ascii " << c << std:...

Templated parameter for a template specialisation?

Hi I've got a static member of a templated class that I want defined for a sub group of classes that are templated ie: template <typename T> class FooT { private: static int ms_id; }; template <typename T> class Foo {}; template<> template<typename T> int FooT< template Foo<T> >::ms_id = 10; Sadly this throws the following error ...

Template specialization for enum

Is it possible to specialize a templatized method for enums? Something like (the invalid code below): template <typename T> void f(T value); template <> void f<enum T>(T value); In the case it's not possible, then supposing I have specializations for a number of types, like int, unsigned int, long long, unsigned long long, etc, then...

Can C++ compiler try different (template T) implementations until it finds one that compiles (for T)?

// First try this: template <class T> T Read(istream& in) { T t; in >> t; return t; } // If there is no operator>>(istream&, T) try this: template <class T> T Read(istream& in) { return T (in); } // If there is no constructor T(istream&) try this: template <class T> T Read(istream& in) { return T::OfStream (in); } // now...

Function Template Specialization on Function Pointers

(C++) I have a sanitization function that I want to run on (traditional) pointer types only. My problem is with function templates I can get as far as limiting the function to only pointers, however because of casting rule differences between function pointers and regular pointers, I run into problems. The Sanitize() function needs ...

C++ optimise class template function when template parameters identical

I've got a template class with a template method within it, giving two template parameters T and U. The operation is quite expensive and is showing up in profiling to be a major use of CPU time. I could optimise it somewhat, but only for the case where T == U (which is fairly common), however I'm not sure on the syntax for doing this... ...

hide function template, declare specializations

This is a followup to http://stackoverflow.com/questions/2050900/c-templates-prevent-instantiation-of-base-template I use templates to achieve function overloading without the mess of implicit type conversions: declare the function template, define desired specializations (overloads). all is well except wrong code does not produce erro...

Function template specialization importance and necessity

I read C++ Primer, and it says function template specialization is an advanced topic, but I am totally lost. Can anybody offer an example why function template specialization is important and necessary? Why don't function templates support partial specialization while class templates do? What's the underlying logic? ...

template specialization for CPPUnit isn't being used

If you've used CPPUnit before, you are probably aware of its assertion_traits class that is templatized to handle arbitrary types. This is what allows it to print the "actual" and "expected" values for non-string types when test cases fail. I have used this with success several times, but for one specific type it isn't working for me. ...

C++: Declaration of template class member specialization

When I specialize a (static) member function/constant in a template class, I'm confused as to where the declaration is meant to go. Here's an example of what I what to do - yoinked directly from IBM's reference on template specialization: ===IBM Member Specialization Example=== template<class T> class X { public: static T v; st...

Doxygen for C++ template class member specialization

When I write class templates, and need to fully-specialize members of those classes, Doxygen doesn't recognize the specialization - it documents only the generic definition, or (if there are only specializations) the last definition. Here's a simple example: ===MyClass.hpp=== #ifndef MYCLASS_HPP #define MYCLASS_HPP template<class T> c...

C++ explicit template specialization of templated constructor of templated class

I have a class like template <class T> struct A{ template <class U> A(U u); }; I would like to write an explicit specialization of this for a declaration like A<int>::A(float); In the following test code, if I comment out the specialization, it compiles with g++. Otherwise, it says I have the wrong number of template parame...

C++ template nontype parameter arithmetic

hello I am trying to specialize template the following way: template<size_t _1,size_t _2> // workaround: bool consecutive = (_1 == _2 - 1)> struct integral_index_ {}; ... template<size_t _1> struct integral_index_<_1, _1 + 1> { // cannot do arithmetic? //struct integral_index_<_1, _2, true> { workaround }; however I get compiler mess...

Class template specializations with shared functionality

I'm writing a simple maths library with a template vector type: template<typename T, size_t N> class Vector { public: Vector<T, N> &operator+=(Vector<T, N> const &other); // ... more operators, functions ... }; Now I want some additional functionality specifically for some of these. Let's say I want functions x() a...

Template specialization within template definition: is this supported for all compilers or standard usage?

This compiled on VS 2008, but it seems like non-standard usage of templates. template <class T> class Foo { public: void bar(Foo<int> arg) { // do some stuff here } // more code ... }; Is there an issue since the template specialization Foo<int> is contained within the definition of its own template class? ...

C++ creating generic template function specialisations

I know how to specialise a template function, however what I want to do here is specialise a function for all types which have a given method, eg: template<typename T> void foo(){...} template<typename T, if_exists(T::bar)>void foo(){...}//always use this one if the method T::bar exists T::bar in my classes is static and has differen...