Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - type traits question

I wish to know if it's possible in C++ to somehow handle the following situations:

Situation 1) (Easily handled)

class BasicFacility { }

template <typename U1, typename U2> class Facility : public BasicFacility { }

Suppose now that we want to have some compilation-time assertion and we want to check if the arbitrary type typename T models the Facility. This is pretty simple:

(boost::is_base_of<BasicFacility, T>::type)

Situation 2) (???)

Now let's assume that in the same situation we just have our template class:

template <typename U1, typename U2> class Facility { }

Obviously we can't use the same solution from situation one, because we can't write statement<Facility, T> (Facility is a template itself).

So, is there a way (maybe, dirty, involving ugly casts, alignment-specific, ANYTHING that might work) to check if some T actually equals some template type without introducing specific empty (auxiliary) base classes (because sometimes you simply can't) ?

Thank you.

like image 779
Yippie-Ki-Yay Avatar asked Dec 16 '22 20:12

Yippie-Ki-Yay


1 Answers

It's pretty straightforward to roll your own test:

template <typename T>
struct is_facility : public boost::false_type { };

template <typename U1, typename U2>
struct is_facility< Facility<U1, U2> > : public boost::true_type { };
like image 58
Marcelo Cantos Avatar answered Dec 31 '22 13:12

Marcelo Cantos