Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;-类型特征问题 我想知道C++中是否有可能处理以下情况:_C++_Templates_Typetraits - Fatal编程技术网

C++;-类型特征问题 我想知道C++中是否有可能处理以下情况:

C++;-类型特征问题 我想知道C++中是否有可能处理以下情况:,c++,templates,typetraits,C++,Templates,Typetraits,情况1)(易于处理) 情况2)(?) 现在让我们假设在相同的情况下,我们只有模板类: template <typename U1, typename U2> class Facility { } 模板类工具{} 显然,我们不能使用情景一中相同的解决方案,因为我们不能编写语句(设施本身就是一个模板) 那么,有没有一种方法(可能是脏的,涉及丑陋的强制转换,特定于对齐方式,任何可能有效的方法)来检查一些T是否实际上等于一些模板类型,而不引入特定的空(辅助)基类(因为有时你根本无法做到)

情况1)(易于处理)

情况2)(?)

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

template <typename U1, typename U2> class Facility { }
模板类工具{}
显然,我们不能使用情景一中相同的解决方案,因为我们不能编写
语句
设施
本身就是一个模板)

那么,有没有一种方法(可能是脏的,涉及丑陋的强制转换,特定于对齐方式,任何可能有效的方法)来检查一些
T
是否实际上等于一些
模板类型
,而不引入特定的空(辅助)基类(因为有时你根本无法做到)


谢谢。

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

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
};
模板
结构资产设施{};//注:空
模板
结构资产设施<设施>{
类型定义设施结果;
};
模板
上课{
typedef AssertFacility::结果不仅仅是一个断言;
公众:
//更多的东西
};

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

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 { };
模板
struct is_facility:public boost::false_type{};
模板
结构是_facility:public boost::true_type{};
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
};
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 { };