Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22804

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.

Upvotes: 5

Views: 474

Answers (2)

sbi
sbi

Reputation: 224039

IIUC, you want to make sure a certain template parameter is an instance of the Facility template. That's simple:

template< typename Policy >
struct some_template; // note: only declared

template< typename U1, typename U1 >
struct some_template< Facility<U1,U2> > {
  // implementation
};

Of course, you could also generalize/formalize that:

template< typename T >
struct AssertFacility {}; // note: empty

template< typename U1, typename U2 >
struct AssertFacility< Facility<U1,U2> > {
  typedef Facility<U1,U2> result_t;
};

template< typename Policy >
class some_class {
  typedef AssertFacility<Policy>::result_t just_an_assertion;
public: 
  // more stuff
};

Upvotes: 4

Marcelo Cantos
Marcelo Cantos

Reputation: 185842

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 { };

Upvotes: 8

Related Questions