你可以在C ++中做自定义运算符吗? [英] Can you make custom operators in C++?

查看:561
本文介绍了你可以在C ++中做自定义运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建自定义运算符,以便您可以执行这样的操作?

  if(Hello,world! containsHello)... 

注意:这是一个单独的问题,好主意...);

解决方案

是的! (well,sort of)



有几个公开的工具可以帮助你。都使用预处理器代码生成来创建实现自定义运算符的模板。这些操作符由一个或多个内置操作符和一个标识符组成。



由于这些操作符实际上不是自定义操作符,而只是操作符重载的技巧,几个警告:




  • 宏是邪恶的。如果你犯了一个错误,编译器将无法完全用于跟踪问题。

  • 即使你获得宏的权利,如果你的操作使用错误或者在操作的定义中,编译器只会稍微有用。

  • 您必须使用有效的标识符作为运算符的一部分。如果你想要一个更像符号的运算符,可以使用 _ o 或类似的简单字母数字。 >


CustomOperators



当我为此目的而在我自己的库上工作时(见下文),我遇到了这个项目。下面是一个创建 avg 运算符的示例:

  #define avg BinaryOperatorDefinition(_op_avg,/)
DeclareBinaryOperator(_op_avg)
DeclareOperatorLeftType(_op_avg,/,double);
inline double _op_avg(double l,double r)
{
return(l + r)/ 2;
}
BindBinaryOperator(double,_op_avg,/,double,double)



IdOp



纯粹的轻浮练习成为我自己对这个问题的接受。这里有一个类似的示例:

  template< typename T& class AvgOp {
public:
T operator()(const T& left,const T& right)
{
return(left + right)/ 2;
}
};
IDOP_CREATE_LEFT_HANDED(< _avg_,>,AvgOp)
#define avg< _avg_>



主要区别




  • CustomOperators支持后缀一元运算符

  • IdOp模板使用引用而不是指针来消除使用免费存储,并允许完全编译时评估

  • IdOp允许您轻松地为同一个根标识符指定多个操作


Is it possible to make a custom operator so you can do things like this?

if ("Hello, world!" contains "Hello") ...

Note: this is a separate question from "Is it a good idea to..." ;)

解决方案

Yes! (well, sort of)

There are a couple publicly available tools to help you out. Both use preprocessor code generation to create templates which implement the custom operators. These operators consist of one or more built-in operators in conjunction with an identifier.

Since these aren't actually custom operators, but merely tricks of operator overloading, there are a few caveats:

  • Macros are evil. If you make a mistake, the compiler will be all but entirely useless for tracking down the problem.
  • Even if you get the macro right, if there is an error in your usage of the operator or in the definition of your operation, the compiler will be only slightly more helpful.
  • You must use a valid identifier as part of the operator. If you want a more symbol-like operator, you can use _, o or similarly simple alphanumerics.

CustomOperators

While I was working on my own library for this purpose (see below) I came across this project. Here is an example of creating an avg operator:

#define avg BinaryOperatorDefinition(_op_avg, /)
DeclareBinaryOperator(_op_avg)
DeclareOperatorLeftType(_op_avg, /, double);
inline double _op_avg(double l, double r)
{
   return (l + r) / 2;
}
BindBinaryOperator(double, _op_avg, /, double, double)

IdOp

What started as an exercise in pure frivolity became my own take on this problem. Here's a similar example:

template<typename T> class AvgOp { 
public: 
   T operator()(const T& left, const T& right) 
   {
      return (left + right) / 2; 
   }
};
IDOP_CREATE_LEFT_HANDED(<, _avg_, >, AvgOp)
#define avg <_avg_>

Key Differences

  • CustomOperators supports postfix unary operators
  • IdOp templates use references rather than pointers to eliminate use of the free store, and to allow full compile-time evaluation of the operation
  • IdOp allows you to easily specify several operations for the same root identifier

这篇关于你可以在C ++中做自定义运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆