user14331070
user14331070

Reputation:

specialization of a template who has a parameter

I got the following template

template<class T, class U, bool(*function)(T)>
struct foo {
/.../
}

How do I create a specialization for it? The spcialization should be like the following

template<>
struct foo<my_type_1, my_type_2,/*how do I handle this parameter?*/>

My main question is how do I handle the parameter of the funcion?

thanks in advance

Upvotes: 1

Views: 63

Answers (3)

Jarod42
Jarod42

Reputation: 218238

Assuming:

// Your primary template
template <class T, class U, bool(*function)(T)>
struct foo;

// Some classes
struct my_type_1;
struct my_type_2;

// A function
bool my_func(my_type_1);

For partial specialization, it would be:

template <bool (*func)(my_type1)>
struct foo<my_type_1, my_type_2, func> { /*..*/};

For full specialization:

template <>
struct foo<my_type_1, my_type_2, &my_func> { /*..*/};

Upvotes: 1

user8108136
user8108136

Reputation: 68

just add your function with my_type_1 as parameter and bool as return:

bool poo(my_type_1 X){
    return 0;
}

so your code must compile:

struct foo<my_type_1, my_type_2, poo>

Upvotes: 1

Alexander Guyer
Alexander Guyer

Reputation: 2203

You handle it like you do any other argument in the template's argument list. T is a class (or other type), so you provide it with such (my_type_1). Same goes for U. The last parameter is a function which accepts an object of type T (which in your specialization is my_type_1) and returns a bool. So provide it with such:

template<class T, class U, bool(*function)(T)>
struct foo {};

struct my_type_1 {};
struct my_type_2 {};

bool my_func(my_type_1 m) {
    return true;
}

template<>
struct foo<my_type_1, my_type_2, my_func> {};

Upvotes: 2

Related Questions