Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ logical operators return value
    text
    copied!<p>Here is some code I'm writing in C++. There's a call to an addAVP() function</p> <pre><code>dMessage.addAVP(AVP_DESTINATION_HOST, peer-&gt;getDestinationHost() || peer-&gt;getHost()); </code></pre> <p>which has two versions: one overloaded in the second parameter to <code>addAVP(int, char*)</code> and another to <code>addAVP(int, int)</code>. I find the C++ compiler I use calls the <code>addAVP(int, int)</code> version which is not what I wanted since <code>getDestinationHost()</code> and <code>getHost()</code> both return <code>char*</code>. </p> <p>Nonetheless the || operator <em>is</em> defined to return bool so I can see where my error is. Bool somehow counts as an integer and this compiles cleanly and calls the second <code>addAVP()</code>.</p> <p>Lately I'm using a lot of dynamically typed languages, i.e. lisp, where the above code is correct can be written without worries. Clearly, clearly the above code in C++ is a big error, but still have some questions:</p> <ol> <li><p>Should I be using this kind of shortcut, i.e. using the ||-operator's return value, at all in C++. Is this compiler dependent? </p></li> <li><p>Imagine that I really, really <em>had</em> to write the nice <code>a || b</code> syntax, could this be done cleanly in C++? By writing an operator redefinition? Without losing performance?</p></li> </ol> <p>As a followup to my original request, or my own answer to 2 :-) I was thinking along the lines of using a class to encapsulate the (evil?) rawpointer:</p> <pre> class char_ptr_w { const char* wrapped_; public: char_ptr_w(const char* wrapped) : wrapped_(wrapped) {} char_ptr_w(char_ptr_w const& orig) { wrapped_=orig.wrapped(); } ~char_ptr_w() {} inline const char* wrapped() const { return wrapped_; } }; inline char_ptr_w operator||(char_ptr_w &lhs, char_ptr_w& rhs) { if (lhs.wrapped() != NULL) return char_ptr_w(lhs.wrapped()); else return char_ptr_w(rhs.wrapped()); }; </pre> <p>Then I could use:</p> <pre> char_ptr_w a(getDestinationHost()); char_ptr_w b(getHost()); addAVP(AVP_DESTINATION_HOST, a || b); </pre> <p>In which this <code>addAVP</code> would be overloaded for <code>char_ptr_w</code>. According to my tests, this generates at most the same assembly code as ternary <code>a?b:c</code> solution, particularly because of the NRVO optimization in the operator, which does <em>not</em>, in most compilers, call the copy-constructor (although you <em>have</em> to include it). </p> <p>Naturally, in this particular example I agree that the ternary solution is the best. I also agree that operator redefinition is something to be taken with care, and not always beneficial. But is there anything conceptually wrong, in a C++ sense, with the above solution? </p>
 

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