型别到型别的转换目的是为了在编译期决定调用哪一部分的代码,型别选择则是以在编译期决定使用何种型别为目的。

Select.h

//在泛化版本中,以Then型别作为nested type
template<bool If, class Then, class Else>
struct Select
{
	typedef Then Result;
};
//在特化版本中(false),以Else型别作为nested type
template<class Then, class Else>
struct Select<false, Then, Else>
{
	typedef Else Result;
};

Select.cpp

#include <iostream>
using namespace std;
#include "Select.h"
int main()
{
	Select<true, int, double>::Result i1 = 1.1;
	Select<false, int, double>::Result i2 = 1.1;
	cout << "i1 = " << i1 << "\n" << "i2 = " << i2 << endl;
}

相关文章:

  • 2021-09-25
  • 2021-11-23
  • 2022-12-23
  • 2021-10-17
  • 2021-05-19
  • 2021-07-21
  • 2021-08-07
  • 2021-06-10
猜你喜欢
  • 2021-09-29
  • 2021-10-14
  • 2022-12-23
  • 2021-11-27
  • 2021-11-08
相关资源
相似解决方案