Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;98从操作员处获得晋升结果_C++_Templates - Fatal编程技术网

C++ C++;98从操作员处获得晋升结果

C++ C++;98从操作员处获得晋升结果,c++,templates,C++,Templates,接下来的一个问题是,是否有一种只使用C++98而不使用boost的好方法?可能不是最好的方法,但这就是我得到的 template <bool value,typename T,typename J> struct conditional{typedef T type;}; template <typename T,typename J> struct conditional<false,T,J>{typedef J type;}; template <


接下来的一个问题是,是否有一种只使用C++98而不使用boost的好方法?

可能不是最好的方法,但这就是我得到的

template <bool value,typename T,typename J>
struct conditional{typedef T type;};

template <typename T,typename J>
struct conditional<false,T,J>{typedef J type;};

template <typename T, typename J>
struct is_equal
{
  enum {value = false};
};

template <typename T>
struct is_equal<T,T>
{
  enum {value = true};
};

namespace
{
template <typename T, typename J, typename K, typename L>
struct math_t
{
  typedef typename conditional<
    is_equal<T,K>::value || is_equal<J,K>::value,
    K,
    L>::type type;
};
}
template <typename T, typename J>
struct math_type
{
  typedef typename math_t<T,J,long double,
  typename math_t<T,J,double,
  typename math_t<T,J,float,
  typename math_t<T,J,long long unsigned int,
  typename math_t<T,J,long long int,
  typename math_t<T,J,long unsigned int,
  typename math_t<T,J,long int,
  typename math_t<T,J,unsigned int,
  int>::type>::type>::type>::type>::type>::type>::type>::type type;
};
模板
结构条件{typedef T type;};
模板
结构条件{typedef J type;};
模板
结构是相等的
{
枚举{value=false};
};
模板
结构是相等的
{
枚举{value=true};
};
名称空间
{
模板
结构数学
{
typedef typename条件<
is|equal::value | is|equal::value,
K
五十> ::类型类型;
};
}
模板
结构数学类型
{
typedef typename math_t::type type;
};

只要您只需要担心标准内置类型,就可以这样做:

template <int> struct plus_helper;
template <> struct plus_helper< 1> { typedef char type; };
template <> struct plus_helper< 2> { typedef signed char type; };
template <> struct plus_helper< 3> { typedef unsigned char type; };
template <> struct plus_helper< 4> { typedef short type; };
template <> struct plus_helper< 5> { typedef unsigned short type; };
template <> struct plus_helper< 6> { typedef int type; };
template <> struct plus_helper< 7> { typedef unsigned int type; };
template <> struct plus_helper< 8> { typedef long type; };
template <> struct plus_helper< 9> { typedef unsigned long type; };
template <> struct plus_helper<10> { typedef float type; };
template <> struct plus_helper<11> { typedef double type; };
template <> struct plus_helper<12> { typedef long double type; };
template <> struct plus_helper<13> { typedef wchar_t type; };

template <typename T1, typename T2>
struct plus {
private:
  static char (&f(char))[1];
  static char (&f(signed char))[2];
  static char (&f(unsigned char))[3];
  static char (&f(short))[4];
  static char (&f(unsigned short))[5];
  static char (&f(int))[6];
  static char (&f(unsigned int))[7];
  static char (&f(long))[8];
  static char (&f(unsigned long))[9];
  static char (&f(float))[10];
  static char (&f(double))[11];
  static char (&f(long double))[12];
  static char (&f(wchar_t))[13];
public:
  typedef typename plus_helper<sizeof(f(*(T1*)0 + *(T2*)0))>::type type;
};

template <typename T1, typename T2>
struct plus<T1 *, T2> {
  typedef T1 *type;
};

template <typename T1, typename T2>
struct plus<T1, T2 *> {
  typedef T2 *type;
};
template struct plus\u helper;
模板结构plus_helper<1>{typedef char type;};
模板结构plus_helper<2>{typedef signed char type;};
模板结构plus_helper<3>{typedef unsigned char type;};
模板结构plus_helper<4>{typedef short type;};
模板结构plus_helper<5>{typedef unsigned short type;};
模板结构plus_helper<6>{typedef int type;};
模板结构plus_helper<7>{typedef unsigned int type;};
模板结构plus_helper<8>{typedef long type;};
模板结构plus_helper<9>{typedef unsigned long type;};
模板结构加上辅助程序{typedef float type;};
模板结构plus_helper{typedef double type;};
模板结构plus_helper{typedef long double type;};
模板结构plus_helper{typedef wchar_t type;};
模板
结构加{
私人:
静态字符(&f(char))[1];
静态字符(&f(有符号字符))[2];
静态字符(&f(无符号字符))[3];
静态字符(&f(short))[4];
静态字符(&f(无符号短))[5];
静态字符(&f(int))[6];
静态字符(&f(unsigned int))[7];
静态字符(&f(长))[8];
静态字符(&f(无符号长))[9];
静态字符(&f(float))[10];
静态字符(&f(double))[11];
静态字符(&f(长双精度))[12];
静态字符(&f(wchar_t))[13];
公众:
typedef typename plus_helper::type type;
};
模板
结构加{
typedef T1*型;
};
模板
结构加{
typedeft2*型;
};
注意,我要求可以添加
T1
T2
。如果不能,则不一定给出错误。还请注意,我没有省略短于-
int
的类型。内置的
+
运算符永远不能返回它们,但它们是自定义
运算符+
的有效返回类型


不幸的是,如果您还需要担心其他类型,例如自定义结构,那么这种方法将不起作用,尽管我很高兴被证明是错误的,但我认为这是不可能的。

我在这里讨论了一个类似的问题:。重新编写我的代码,因为这似乎是您所追求的。详细解释请参见参考答案

// typedef eiher to A or B, depending on what integer is passed
template<int, typename A, typename B>
struct cond;

#define CCASE(N, typed) \
  template<typename A, typename B> \
  struct cond<N, A, B> { \
    typedef typed type; \
  }

CCASE(1, A); CCASE(2, B);
CCASE(3, int); CCASE(4, unsigned int);
CCASE(5, long); CCASE(6, unsigned long);
CCASE(7, float); CCASE(8, double);
CCASE(9, long double);

#undef CCASE

// for a better syntax...
template<typename T> struct identity { typedef T type; };

// different type => figure out common type
template<typename A, typename B>
struct promote {
private:
  static A a;
  static B b;

  // in case A or B is a promoted arithmetic type, the template
  // will make it less preferred than the nontemplates below
  template<typename T>
  static identity<char[1]>::type &check(A, T);
  template<typename T>
  static identity<char[2]>::type &check(B, T);

  // "promoted arithmetic types"
  static identity<char[3]>::type &check(int, int);
  static identity<char[4]>::type &check(unsigned int, int);
  static identity<char[5]>::type &check(long, int);
  static identity<char[6]>::type &check(unsigned long, int);
  static identity<char[7]>::type &check(float, int);
  static identity<char[8]>::type &check(double, int);
  static identity<char[9]>::type &check(long double, int);

public:
  typedef typename cond<sizeof check(0 ? a : b, 0), A, B>::type
    type;
};

// same type => finished
template<typename A>
struct promote<A, A> {
  typedef A type;
};
//根据传递的整数,将eiher类型定义为A或B
模板
结构状态;
#定义CCASE(N,已键入)\
模板\
结构条件{\
类型定义类型\
}
CCASE(1,A);CCASE(2,B);
CCASE(3,国际);CCASE(4,无符号整数);
CCASE(5个,长);CCASE(6,无符号长);
CCASE(7,浮动);CCASE(8,双);
CCASE(9,长双);
#未定义CCASE
//为了更好的语法。。。
模板结构标识{typedef T type;};
//不同类型=>找出常见类型
模板
结构升级{
私人:
静态A;
静态B;
//如果A或B是升级的算术类型,则模板
//将使其比下面的非模板更不受欢迎
模板
静态标识::类型和检查(A,T);
模板
静态标识::类型和检查(B,T);
//“提升的算术类型”
静态标识::类型和检查(int,int);
静态标识::类型和检查(unsigned int,int);
静态标识::类型和检查(长,int);
静态标识::类型和检查(无符号长,int);
静态标识::类型和检查(浮点、整数);
静态标识::类型和检查(双精度,整数);
静态标识::类型和检查(长双精度,int);
公众:
typedef typename cond::type
类型;
};
//相同类型=>finished
模板
结构升级{
类型定义类型;
};
比如说

int main() {
  promote<char, short>::type a;
  int *p0 = &a;

  promote<float, double>::type b;
  double *p1 = &b;

  promote<char*, string>::type c;
  string *p2 = &c;
}
intmain(){
a型;
int*p0=&a;
b型;
双*p1=&b;
c型;
字符串*p2=&c;
}

考虑添加
长整数和
无符号整数时会发生什么情况。结果取决于实现,但在我的系统中,它是
unsigned long int
,这不是模板提供的结果。有趣的是,我们最终得到了非常类似的解决方案:)