jeudi 14 novembre 2019

How to create a specialized a function template for a custom templated object?

So I am not sure if this is possible but I want to have specialized function for a templated custom object.

I have custom objects:

template<unsigned N>
struct A {};

struct B {};

Now I am trying to define specialized functions to handle different structs. Specifically I want to create a function to handle objects of type A for any N. However, I am having trouble finding the correct syntax for this. The only way I have gotten this to work is by using explicit specialization.

// Default function handler
template<typename T>
void foo(T t)
{
    std::cout << "Default handler" << std::endl;
}

// This part will not compile, syntax is wrong
template<typename T<unsigned N>>
void foo(T<N> t)
{
    std::cout << "Specialized handler" << std::endl;
}

template<typename>
void foo(A<5> t)
{
    std::cout << "this is called properly when 5 is specified" << std::endl;
}

What would the correct syntax be for having a specialized function for all types of A objects?

Aucun commentaire:

Enregistrer un commentaire