C++ 如何将类的某些typedef传递给模板

C++ 如何将类的某些typedef传递给模板,c++,templates,instantiation,C++,Templates,Instantiation,在以下任务中,我想创建一个模板,该模板只接受以下类CDATA格式中定义的typedefs: class CDataFormat{ public: typedef unsigned short element_t; typedef unsigned int accumulation_t; typedef double division_t; }; 现在,以下实现工作正常 template<typename DF, int SIZE> class CFilt

在以下任务中,我想创建一个模板,该模板只接受以下类CDATA格式中定义的typedefs:

class CDataFormat{
public:
    typedef unsigned short  element_t;
    typedef unsigned int accumulation_t;
    typedef double division_t;
}; 
现在,以下实现工作正常

template<typename DF, int SIZE>
class CFilter{
private:
    DF m_memberName[SIZE];
public: 
    void whatever(){
    //CFilter<CDataFormat::division_t, 8> smth; // Just a small test
    }
};
模板
类过滤器{
私人:
DF m_成员名称[大小];
公众:
无效{
//CFilter smth;//只是一个小测试
}
};
但是,不能确保模板只接受CDATA格式的成员


如何操作?

您可以使用
static\u assert
报告误用:

template <typename DF, int SIZE>
class CFilter{
static_assert(std::is_same<CDataFormat::element_t, DF>::value
           || std::is_same<CDataFormat::accumulation_t, DF>::value
           || std::is_same<CDataFormat::division_t, DF>::value, "Unexpected type");

private:
    DF m_memberName[SIZE];
};
模板
类过滤器{
静态断言(std::is_same::value)
||std::值是否相同
||std::is_same::value,“意外类型”);
私人:
DF m_成员名称[大小];
};

您可以使用
static\u assert
报告误用:

template <typename DF, int SIZE>
class CFilter{
static_assert(std::is_same<CDataFormat::element_t, DF>::value
           || std::is_same<CDataFormat::accumulation_t, DF>::value
           || std::is_same<CDataFormat::division_t, DF>::value, "Unexpected type");

private:
    DF m_memberName[SIZE];
};
模板
类过滤器{
静态断言(std::is_same::value)
||std::值是否相同
||std::is_same::value,“意外类型”);
私人:
DF m_成员名称[大小];
};

您可以使用helper类和
static\u assert
来满足您的需要

#include <type_traits>

class CDataFormat{
public:
    typedef unsigned short  element_t;
    typedef unsigned int accumulation_t;
    typedef double division_t;

}; 

template <typename T> struct is_valid_type
{
   static const bool value = std::is_same<T, CDataFormat::element_t>::value ||
                             std::is_same<T, CDataFormat::accumulation_t>::value ||
                             std::is_same<T, CDataFormat::division_t>::value ;
};

template<typename DF, int SIZE>
class CFilter{
  static_assert(is_valid_type<DF>::value, "Unsupported type");
  private:
    DF m_memberName[SIZE];
  public: 
    void whatever(){
    }
};
#包括
类CDATA格式{
公众:
typedef无符号短元素\u t;
typedef无符号整数累加;
typedef双除法;
}; 
模板结构是\u有效\u类型
{
静态常量布尔值=标准::是否相同::值||
std::值是否相同||
std::is_same::value;
};
模板
类过滤器{
静态断言(是有效的类型::值,“不支持的类型”);
私人:
DF m_成员名称[大小];
公众:
无效{
}
};
那么,

CFilter<CDataFormat::element_t, 10> f1;
CFilter f1;
除了使用

CFilter<int, 20> f2;
CFilter f2;

将导致编译时错误。

您可以使用帮助器类和
静态断言来满足您的需要

#include <type_traits>

class CDataFormat{
public:
    typedef unsigned short  element_t;
    typedef unsigned int accumulation_t;
    typedef double division_t;

}; 

template <typename T> struct is_valid_type
{
   static const bool value = std::is_same<T, CDataFormat::element_t>::value ||
                             std::is_same<T, CDataFormat::accumulation_t>::value ||
                             std::is_same<T, CDataFormat::division_t>::value ;
};

template<typename DF, int SIZE>
class CFilter{
  static_assert(is_valid_type<DF>::value, "Unsupported type");
  private:
    DF m_memberName[SIZE];
  public: 
    void whatever(){
    }
};
#包括
类CDATA格式{
公众:
typedef无符号短元素\u t;
typedef无符号整数累加;
typedef双除法;
}; 
模板结构是\u有效\u类型
{
静态常量布尔值=标准::是否相同::值||
std::值是否相同||
std::is_same::value;
};
模板
类过滤器{
静态断言(是有效的类型::值,“不支持的类型”);
私人:
DF m_成员名称[大小];
公众:
无效{
}
};
那么,

CFilter<CDataFormat::element_t, 10> f1;
CFilter f1;
除了使用

CFilter<int, 20> f2;
CFilter f2;
将导致编译时错误