Recently, there ran a question about the sequence of operators ++++ and ---- . Inspired by ...

Sutter in “New Challenges” has a funny task 33 about how many identical characters of a given operator can be used in the correct C ++ code in a row (comments, preprocessor, macros, literals are not considered).

My question is - did the answer to this task change with the release of the C ++ 11/14 standard?

As I understand it, now the sequence >>>> becomes infinite - now there is no need to insert a space in the templates ...

The rest of the operators should seem to remain unchanged.

But now in C ++, the && construct has acquired another meaning. In this regard, the question is: did Sutter's decision remain unchanged from five in a row or can he now be surpassed ?

This is not a competition, but if someone surpasses Satter's solutions, it will be interesting to see. For reference:
+ , - ! , ~ , * - trivial infinite sequences
& - five pieces
< , | - four
... - not even funny :)

Yes, get two %% or there ^^ also simple:

 class X { public: void operator%(int); }; typedef void(X::*F)(int); void operator%(F,X){} int main(int argc, const char * argv[]) { X x; &X::operator%%x; } 
  • eight
    Another star must be in the endless record: ru.stackoverflow.com/a/520258/178988 . - Qwertiy
  • Oh, yes, of course. But, by the way, Sutter didn't have it (!) - Harry
  • I did not understand the remark about the ellipsis. But 6 points in a row can be . - αλεχολυτ
  • @alexolut Well, in the sense that ... are not three point operators . As for the six points - do not be shy, it's interesting to meet you :) - Harry

2 answers 2

In my opinion, the use of new semantics for && in c ++ 11 will not allow building a longer chain than Sutter (5 characters in a row). The reason why I think so is based on possible new usage contexts && :

  • specifying an rvalue reference for a local variable type:

     R&& r = makeR(); 
  • Using the universal link in the template function:

     template<typename T> void f(T&& param); 
  • use a universal reference for a local auto variable:

     auto&& r = f(something); 
  • overloading member functions for rvalue / lvalue objects:

     struct S { void f() & {} void f() && {} }; 

In all mentioned contexts, && used when declaring an entity. Indicate here more than 2 & in a row, it is not possible. The compiler will tell you not to link to the link. For example, for clang :

 'variable' declared as a reference to a reference 

For a template function or overloaded for an rvalue / lvalue, the type indication is not required when calling, but is determined from the type of the expression. Those. & not called at all. And if they are used, then only for an explicit cast, where more than two are & still cannot be specified. For example:

 static_cast<T&&>(t); 

If you try to specify more, we get the link link error that was previously mentioned.

So To break Sutter's record for & appearance of new link semantics in c ++ 11 does not allow.

  • And the promised six points? :) - Harry
  • @Harry may a little later, if no one writes :) But, in general, everything you need to eat is by reference from the comment. - αλεχολυτ

Six points ...... Based on the message with enSO.

We have the code:

 template <typename T> struct X { }; template <typename T, typename ... U> struct X<T(U......)> { }; 

Here there is a pattern of structure X and a partial specialization of this pattern for parameterization of a type by a function that takes several parameters U...... and returns type T

U...... is the same as U... ... or U..., ... U... is a nameless set of types passed to the pattern, and the next ... is a multiple dot to ensure the transfer of a variable number of parameters. Thus, to instantiate a template specialization, you can use, for example, the following code:

 X<int(double, char, ...)> x; 

x parameterized by a function that returns an int and takes at least 2 parameters: one is of the double type, the second is of the char type, then any number of additional parameters can be used to access which you need to use functions from the <cstdarg> header file .

When using the variant without an explicit comma between the three- dotted points, the clang gives, explaining the essence, a warning :

 '...' in this location creates a C-style varargs function [-Wambiguous-ellipsis]