你能在 C++ 中制作自定义运算符吗?

是否可以制作自定义运算符以便您可以执行此类操作?

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:

  • 宏是邪恶的.如果你犯了一个错误,编译器在追踪问题方面几乎毫无用处.
  • 即使您正确使用了宏,如果您在使用运算符或定义操作时出现错误,编译器也只会稍微提供一些帮助.
  • 您必须使用有效标识符作为运算符的一部分.如果您想要更像符号的运算符,您可以使用 _o 或类似的简单字母数字.
  • 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.

当我为此目的开发自己的库时(见下文),我遇到了这个项目.以下是创建 avg 运算符的示例:

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

什么开始是纯粹的轻浮练习成为我自己对这个问题的看法.下面是一个类似的例子:

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_>

主要区别

  • CustomOperators 支持后缀一元运算符
  • IdOp 模板使用引用而不是指针来消除对免费存储的使用,并允许对操作进行完整的编译时评估
  • IdOp 允许您轻松地为同一个根标识符指定多个操作

相关文章