c+中的安全整数转换+; < >我想用C++模板创建一个简单的整数范围检查器和转换器。 代码如下所示: // D is the "destination" type and S the "source" type template <class D, class S> inline D SafeConvert( S value ); template <class S> inline int SafeConvert<int>( S value ) { ASSERT( value >= S(INT_MIN) && value <= S(INT_MAX) ); return int(value); } /// error C2768: 'SafeConvert' : illegal use of explicit template arguments template <class S> inline size_t SafeConvert<size_t>( S value ) { ASSERT( value >= S(0) && value <= S(size_t(-1)) ); return size_t(value); } /// error C2768: 'SafeConvert' : illegal use of explicit template arguments // ... void test() { size_t v = INT_MAX+1; int iv = SafeConvert<int>(v); }

c+中的安全整数转换+; < >我想用C++模板创建一个简单的整数范围检查器和转换器。 代码如下所示: // D is the "destination" type and S the "source" type template <class D, class S> inline D SafeConvert( S value ); template <class S> inline int SafeConvert<int>( S value ) { ASSERT( value >= S(INT_MIN) && value <= S(INT_MAX) ); return int(value); } /// error C2768: 'SafeConvert' : illegal use of explicit template arguments template <class S> inline size_t SafeConvert<size_t>( S value ) { ASSERT( value >= S(0) && value <= S(size_t(-1)) ); return size_t(value); } /// error C2768: 'SafeConvert' : illegal use of explicit template arguments // ... void test() { size_t v = INT_MAX+1; int iv = SafeConvert<int>(v); },c++,templates,C++,Templates,我的问题是如何告诉编译器我只想专门化D类 谢谢。编写一个结构SafeConverter,该结构由SafeConvert使用。比部分专门化更好的方法是使用std::numeric\u limits,甚至是boost::numeric\u cast,后者已经以更复杂的方式实现了范围检查 后者可按以下方式实施: template<typename T, typename S> struct numeric_converter { static T convert(const S&

我的问题是如何告诉编译器我只想专门化D类


谢谢。

编写一个结构
SafeConverter
,该结构由
SafeConvert
使用。比部分专门化更好的方法是使用
std::numeric\u limits
,甚至是
boost::numeric\u cast
,后者已经以更复杂的方式实现了范围检查

后者可按以下方式实施:

template<typename T, typename S>
struct numeric_converter {
  static T convert(const S& val);
}
template<typename T, typename S>
T numeric_cast(const S& val) {
  typedef numeric_converter<T, S> converter;
  return converter::convert(val);
}
模板
结构数字转换器{
静态T转换(常数S&val);
}
样板
T数字转换(常数S&val){
typedef数字转换器;
返回转换器::convert(val);
}

编写一个由
SafeConvert
使用的结构
SafeConvert
。比部分专门化更好的方法是使用
std::numeric\u limits
,甚至是
boost::numeric\u cast
,后者已经以更复杂的方式实现了范围检查

后者可按以下方式实施:

template<typename T, typename S>
struct numeric_converter {
  static T convert(const S& val);
}
template<typename T, typename S>
T numeric_cast(const S& val) {
  typedef numeric_converter<T, S> converter;
  return converter::convert(val);
}
模板
结构数字转换器{
静态T转换(常数S&val);
}
样板
T数字转换(常数S&val){
typedef数字转换器;
返回转换器::convert(val);
}

您不能部分专门化函数模板。您需要使用类包装器来模拟它,或者使用标准函数重载。模仿的一个例子:

template <typename T1, typename T2>
struct processor;

template < typename T1, typename T2 >
T1 fun(T2 t2) { return processor<T1,T2>::apply(t2); }

template < typename T2 >
struct processor<int,T2>
{
   static int apply(T2 t2) { .... }
};

...etc...
模板
结构处理器;
模板
T1乐趣(T2){返回处理器::应用(T2);}
模板
结构处理器
{
静态int应用(T2){….}
};
等

您不能部分专门化函数模板。您需要使用类包装器来模拟它,或者使用标准函数重载。模仿的一个例子:

template <typename T1, typename T2>
struct processor;

template < typename T1, typename T2 >
T1 fun(T2 t2) { return processor<T1,T2>::apply(t2); }

template < typename T2 >
struct processor<int,T2>
{
   static int apply(T2 t2) { .... }
};

...etc...
模板
结构处理器;
模板
T1乐趣(T2){返回处理器::应用(T2);}
模板
结构处理器
{
静态int应用(T2){….}
};
等

我认为,只需编写
SafeConvert
而不是
SafeConvert
,就可以专门处理第二个参数。Noah Roberts在函数与类型的部分专业化方面也是正确的。

只需编写
SafeConvert
,而不是
SafeConvert
,我认为,只专门化第二个参数。诺亚·罗伯茨(Noah Roberts)在函数与类型的部分专业化方面也是正确的。

这将是一个麻烦,也是一个地狱

通常,我建议使用
数值限制

template <class D, class S>
D SafeConvert(S value)
{
  ASSERT(value >= std::numeric_limits<D>::min()
      && value <= std::numeric_limits<D>::max());
  return static_cast<D>(value);
}
模板
D安全转换(S值)
{
断言(值>=std::numeric_limits::min()

&&值这将是一个麻烦,一个地狱维持

通常,我建议使用
数值限制

template <class D, class S>
D SafeConvert(S value)
{
  ASSERT(value >= std::numeric_limits<D>::min()
      && value <= std::numeric_limits<D>::max());
  return static_cast<D>(value);
}
模板
D安全转换(S值)
{
断言(值>=std::numeric_limits::min()

&&我不会否定,因为我几乎说了同样的话,但你应该先检查你的答案。然后你可能会记得为什么你不能那样做。SO的问题是答案弹出得太快,以至于我通常没有时间在发布前思考我的答案,对不起。我不会否定,因为我几乎说了同样的话但是你应该先检查你的答案。然后你可能会记得为什么你不能那样做。SO的问题是答案弹出得太快,以至于我通常没有时间在发布前思考我的答案,对不起。反过来说,你想专门研究第一个(目的地)争论。反过来说,你想专门研究第一个(目的地)参数。数值限制和boost::Numeric_cast是一种方法。这种断言在S的最大值更大的情况下和D的最大值更大的情况下都有效吗?或者隐式转换会在一个方向上把它搞砸吗?@JosephGarvin:我很有信心
boost::Numeric_cast
对于这里介绍的自定义解决方案,我恐怕不太清楚隐式转换。数字限制和boost::Numeric_cast是一种方法。这种断言在S的最大值更大和D的最大值更大的情况下都有效吗?或者隐式转换会在一个方向上把它搞砸吗?@JosephGarvin:我很确定对于这里介绍的自定义解决方案,我恐怕不知道如何将隐式转换牢记在心。您可能想试试您可能想试试