Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a general argument to function using template

Tags:

c++

templates

There are some classes and their relationships are as follows:

class X:A,B
class Y:A,B
class Z:A,B

I want to pass a general type that will inherit from A and B to testFunction using template. My code is as follows:

template<template T:public A, public B>
void testFunction(T generalType)
{
    //do something
}

but my compiler told me that it's error template. How could I fix it?

like image 907
rontgen Avatar asked Dec 11 '22 17:12

rontgen


1 Answers

The standard method to conditionally define a template is std::enable_if<condition>. In this case, you want to check the condition std::is_base_of<A,T>::value && std::is_base_of<B,T>::value

like image 164
MSalters Avatar answered Dec 31 '22 16:12

MSalters