在模板实例化中限制参数类型

Fra*_*ank 5 c++ templates boost

如果我的库的用户尝试使用不合适的类型实例化模板,我试图触发编译时错误.我实施了:

template <typename T>
struct good_type { enum { value = false }; };

template <>
struct good_type<string> { enum { value = true }; };

template <>
struct good_type<int64_t> { enum { value = true }; };

template <typename T>
struct X
{
  BOOST_STATIC_ASSERT(good_type<T>::value);
};

int main(int argc, char** argv)
{
  X<string> x1; 
  X<int64_t> x2;
  X<float> x3; 
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,但我从gcc得到的消息有点令人惊讶:

error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>' 
Run Code Online (Sandbox Code Playgroud)

我应该使用不同的Boost宏吗?有一个更好的方法吗?

谢谢!

Naw*_*waz 5

您可以使用boost::enable_if以及类型列表.

定义一个类型列表,其中包含您要支持的所有 类型,并编写一些元函数,以检查列表中是否存在给定类型,然后传递元函数返回的值enable_if,以便启用/禁用该类.


好吧,我写了一个演示代码.boost::enable_if虽然它没有使用(那是为了你试验).

首先是框架:

////////////////////////////////////////////////////////////
//framework

struct null_type {};

template<typename H, typename  T=null_type>
struct typelist
{
   typedef H Head;
   typedef T Tail;
};

template<typename T, typename TList> struct exists;

template<typename T, typename Tail> 
struct exists<T, typelist<T, Tail> >
{
    static const bool value = true;
};

template<typename T, typename Head, typename Tail> 
struct exists<T, typelist<Head, Tail> >
{
    static const bool value = false || exists<T, Tail>::value;
};

template<typename T> 
struct exists<T, null_type >
{
    static const bool value = false;
};

template<bool>
struct compile_time_error;

template<>
struct compile_time_error<true> {};
Run Code Online (Sandbox Code Playgroud)

-

现在遵循测试代码:

//////////////////////////////////////////////////////////////
//usage

typedef typelist<int> t1;
typedef typelist<short, t1> t2;
typedef typelist<char, t2> t3;
typedef typelist<unsigned char, t3> t4;

typedef t4 supported_types;//supported_types: int, short, char, unsigned char

template<typename T>
struct X
{
    compile_time_error<exists<T,supported_types>::value> unsupported_type_used;
};

int main() {

 //testing if exists<> work or not!
 cout <<(exists<int,supported_types>::value)<< endl;        //should print 1
 cout <<(exists<unsigned int,supported_types>::value)<<endl;//should print 0
 cout <<(exists<char,supported_types>::value)<< endl;       //should print 1
 cout <<(exists<long,supported_types>::value)<< endl;       //should print 0

 X<int> x1;   //okay - int is supported!
 //X<long> x2;  //error - long is unsupported! 
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译完全正常(ideone),并给出此输出(对于cout语句):

1
0
1
0
Run Code Online (Sandbox Code Playgroud)

但是,如果您取消注释X<long> x2;上面代码中的行,它将无法编译,因为long它是不受支持的类型.它给出了这个错误,易于阅读和理解(ideone):

prog.cpp:在'X'的实例化中:
prog.cpp:68:从这里实例化
prog.cpp:56:错误:'X :: unsupported_type_used'具有不完整类型
prog.cpp:38:错误:'struct compile_time_error的声明"

希望这对你有所帮助.


现在你可以编写一个名为的模板enable_if_supported,它带有两个类型的参数:Tsupported_types.您可以从以下位置派生您的课程enable_if_supported:

template<typename T>
struct X : enable_if_supported<T, supported_types>
{
   //your code
};
Run Code Online (Sandbox Code Playgroud)

这看起来有点干净.enable_if_supported类模板现在在框架部分中定义.请在此处查看:http://www.ideone.com/EuOgc