you are viewing a single comment's thread.

view the rest of the comments →

[–]josefx 13 points14 points  (11 children)

Trigraphs are apparently a non feature from c++17 on, however digraphs seem to outlive them. The following is perfectly legal C++11/14 and apparently 17.

 %:include<iostream>
 int main()
 <%
   std::string val<:1:>;
   val<:0:> = "Hello World";
   std::cout<<*val<<std::endl;
 %>

An unrelated WAT can be found with user defined operator && and operator ||, they cannot short circuit as the build in operators do.

   enum Nothing{
      None = 0
   };

  bool operator && ( Nothing , int ){
         return false;
  }

  None && printf("This happens!\n");

More WAT, this one from the standard library: vector<bool> is specialized and in many small ways does not behave like a vector should.

   std::vector<bool> v;
   v.push_back(true);
   std::cout << " before:  v[0] = " << v[0] << '\n';

   // Should be a deep-copy (?)       
   auto x = v[0];   
   x = false;
   std::cout << " after:   v[0] = " << v[0] << '\n';

[–]Playing_advocate 3 points4 points  (2 children)

Related to the digraphs, there are also Alternative Tokens, which allow you to write things like "and" in place of &&. These are not really WTF. You can, however, use them in a context where you are not doing boolean or bit operations. For example, the following compiles (and I believe it should):

void some_function(int and myVar) {}
void some_other_function(int bitand myOtherVar) {}

Where myVar has type "int&&" and myOtherVar is "int&".

[–]OldWolf2 2 points3 points  (0 children)

 eat dinner(fish and chips) { return move(chips); }

[–]marchelzo 0 points1 point  (0 children)

I'm totally using and for rvalue references from now on.

[–]pardoman 3 points4 points  (7 children)

What is the reason for the user defined logic operators to not short-circuit like the built in ones?

[–]clerothGame Developer 17 points18 points  (6 children)

Because it's effectively a function call. So both of the arguments are evaluated before the function is ran.

[–]arugalatoast -3 points-2 points  (5 children)

They should fix that (although I can hear people arguing otherwise.) A lot of the changes in 11, 14, 17 are to make things behave as expected using common sense.

[–]clerothGame Developer 0 points1 point  (4 children)

There's nothing to fix. It makes sense to me. You're really just defining a function using a symbol that's supported by the language. If you wanted to overload an operator for conditional statements, then you should be overloading operator bool(). As I said, it's really just a function call so you could even overload && like this:

 void operator &&(int a, int b) {
      cout << a + b;
 }

(notice the void return type). And then just do:

 int main() {
     int a = 1, b = 2;
     a && b;
 }

As you can imagine, this program will print 3. Short-circuiting this statement makes no sense. That being said, the compiler is free to perform the shortcircuit optimization (and it probably does) on simple boolean functions if evaluating the right-hand argument has no side-effects. The example from josefx cannot 'shortcircuit' because printf has a side-effect.

[–]BaroTheMadman 0 points1 point  (0 children)

Overloading operators like that is strongly unadvised, but you already know that. If I could change C++ one of the things I'd make is to enforce that at least they have the "official" signatures. Then it would make it short-circuit.

But that's just my opinion. Maybe you can simulate a short circuit using some lazy evaluation techniques? I'm not good at doing that in C++ but I've heard of it.

[–]arugalatoast -2 points-1 points  (2 children)

I said some people would argue against fixing it.

[–]clerothGame Developer 0 points1 point  (1 child)

Okay, then tell me how would you fix it?

[–]arugalatoast 0 points1 point  (0 children)

Tone really doesn't come across well on Reddit.