久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

你能在 C++ 中制作自定義運算符嗎?

Can you make custom operators in C++?(你能在 C++ 中制作自定義運算符嗎?)
本文介紹了你能在 C++ 中制作自定義運算符嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

是否可以制作自定義運算符以便您可以執行此類操作?

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 允許您輕松地為同一個根標識符指定多個操作

這篇關于你能在 C++ 中制作自定義運算符嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 欧美性一区二区三区 | 成人三级在线播放 | 国产精品一区二区三区久久久 | 亚洲精品视频播放 | 91精品国产一区二区三区 | 高清一区二区三区 | 久久噜噜噜精品国产亚洲综合 | www.日本国产 | 中文字幕在线观看一区二区 | 日韩资源| 91综合网 | 新av在线| 在线观看免费av网 | 一区二区三区不卡视频 | 中文字幕视频一区 | 精品国产乱码久久久久久蜜退臀 | 伊人色综合久久久天天蜜桃 | 久久精品国产久精国产 | 亚州一区二区三区 | 日韩欧美三区 | 国产精品久久久久aaaa九色 | 亚洲美女天堂网 | 一区二区三区四区在线视频 | 国产一区在线免费观看 | 国产精品久久国产精品久久 | 亚洲xxxxx | 爱综合| 欧美精品在线视频 | 在线黄av | 成人国产精品视频 | 一区二区三区免费 | 亚洲天堂网站 | 欧美一区二区三区小说 | 中文字幕在线三区 | 365夜爽爽欧美性午夜免费视频 | 黄色一级大片在线观看 | 亚洲综合三区 | 成年人的视频免费观看 | 日韩av在线一区二区 | 免费黄色网址视频 | 亚洲自拍偷拍欧美 |