4

我想知道在 C++ 中是否有可能以某种方式处理以下情况:

情况1) (容易处理)

class BasicFacility { }

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

现在假设我们想要一些编译时断言,并且我们想要检查任意类型是否typename TFacility. 这很简单:

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

情况2) (???)

现在让我们假设在同样的情况下我们只有我们的模板类:

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

显然我们不能使用与情况一相同的解决方案,因为我们不能编写statement<Facility, T>Facility本身就是一个模板)。

那么,有没有一种方法(可能是肮脏的,涉及丑陋的演员表,特定于对齐方式,任何可能有效的方法)来检查 someT实际上是否等于 sometemplate type而无需引入特定的空(辅助)基类(因为有时你根本不能)?

谢谢你。

4

2 回答 2

8

推出自己的测试非常简单:

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 { };
于 2010-10-18T21:32:32.157 回答
4

IIUC,您要确保某个模板参数是模板的一个实例Facility。这很简单:

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

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

当然,您也可以概括/形式化:

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
};
于 2010-10-18T21:28:20.717 回答