C++ 数字是零和一

C++ 数字是零和一,c++,itk,C++,Itk,可以使用itk::NumericTraits获取某种类型的0和1。因此,我们可以在野外看到这种代码: const PixelType ZERO = itk::NumericTraits<PixelType>::Zero; const PixelType ONE = itk::NumericTraits<PixelType>::One; 但这是否完全等同?我认为演员阵容是在编译过程中完成的,所以两个版本在速度上应该是相同的。如果是这样,为什么会有人想使用itk::Nume

可以使用
itk::NumericTraits
获取某种类型的0和1。因此,我们可以在野外看到这种代码:

const PixelType ZERO = itk::NumericTraits<PixelType>::Zero;
const PixelType ONE = itk::NumericTraits<PixelType>::One;

但这是否完全等同?我认为演员阵容是在编译过程中完成的,所以两个版本在速度上应该是相同的。如果是这样,为什么会有人想使用
itk::NumericTraits
获得0和1?一定有一个我没有看到的优势。

特征通常在泛型编程环境中使用/有用。它在STL中被大量使用

让我们考虑一下你的代码>数字特性<代码>如下:

template <typename PixelT>
struct NumericTraits {
  static const int  ZERO = 0;
  static const int  ONE = 1;
};
你有什么想法和用处吗?现在,它的另一个好处是将值转换为类型,然后将其用于标记分派:

void func(int some_val, std::true_type) {....}
void func(int some_val, std::false_type) {.....}

And call it like:

func(42, typename std::conditional<NumericTraits<PixelType>::ONE == 1, std::true_type, std::false_type>::type());
void func(int some_val,std::true_type){..}
void func(int some_val,std::false_type){….}
把它叫做:
func(42,typename std::conditional::type());
调用哪个重载是在编译时决定的,通过可能提高性能,您可以不用再执行
if-else
检查:)

template <>
struct NumericTraits<SpecialPixel>{
  static const int ZERO = 10;
  static const int ONE = 20;
};
void func(int some_val, std::true_type) {....}
void func(int some_val, std::false_type) {.....}

And call it like:

func(42, typename std::conditional<NumericTraits<PixelType>::ONE == 1, std::true_type, std::false_type>::type());