Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Yes! (well, sort of)</h2> <p>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.</p> <p>Since these aren't actually custom operators, but merely tricks of operator overloading, there are a few caveats:</p> <ul> <li>Macros are evil. If you make a mistake, the compiler will be all but entirely useless for tracking down the problem.</li> <li>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.</li> <li>You must use a valid identifier as part of the operator. If you want a more symbol-like operator, you can use <code>_</code>, <code>o</code> or similarly simple alphanumerics.</li> </ul> <h3><a href="http://www.codeproject.com/KB/cpp/customoperators.aspx" rel="noreferrer">CustomOperators</a></h3> <p>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 <code>avg</code> operator:</p> <pre><code>#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) </code></pre> <h3><a href="http://cogwheel.info/idop/" rel="noreferrer">IdOp</a></h3> <p>What started as <a href="http://blog.cogwheel.info/2009/10/from-frivolity-functionality-follows.html" rel="noreferrer">an exercise in pure frivolity</a> became my own take on this problem. Here's a similar example:</p> <pre><code>template&lt;typename T&gt; class AvgOp { public: T operator()(const T&amp; left, const T&amp; right) { return (left + right) / 2; } }; IDOP_CREATE_LEFT_HANDED(&lt;, _avg_, &gt;, AvgOp) #define avg &lt;_avg_&gt; </code></pre> <p><H3>Key Differences</h3></p> <ul> <li>CustomOperators supports postfix unary operators</li> <li>IdOp templates use references rather than pointers to eliminate use of the free store, and to allow full compile-time evaluation of the operation</li> <li>IdOp allows you to easily specify several operations for the same root identifier</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload