1

Topic: What for void as default template parameter?

Somewhere in std:: template <class T = void> struct minus {constexpr T operator () (const T& x, const T& y) const;}; operator () returns x - y.

2

Re: What for void as default template parameter?

What for? It is obvious that so record turns out much more shortly and more clearly. After all with a default argument it is possible to write simply in usage place std:: minus <> () and then the compiler itself substitutes void as template parameter. Actually instead of void it would be possible to select any other type which program part (approximately does not meet in was informatively as it is made with type std:: piecewise_construct_t). But void here explicitly is better approaches for a role for at once it is clear that constexpr T operator () (const T& x, const T& y) const it is not meaningful with T == void and even casually has no possibility to be compiled, and that this code, and appropriate specialization std:: minus <> will work generally not.

3

Re: What for void as default template parameter?

Hello, watchmaker, you wrote: W> But void here explicitly is better approaches for a role for at once it is clear that constexpr T operator () (const T& x, const T& y) const it is not meaningful with T == void and even casually has no possibility to be compiled... This operator is all the same declared sample, and it  are not connected in any way to sample parameters of the class. I guess that specialization possibility, for something here was put, but scenarios of usage of this possibility for me are shrouded in mystery.

4

Re: What for void as default template parameter?

Hello, rg45, you wrote: R> This operator is all the same declared sample, and it  are not connected in any way to sample parameters of the class. I guess that specialization possibility, for something here was put, but scenarios of usage of this possibility for me are shrouded in mystery. Something my telepathy did not consult and did not understand that for guesses you are tormented = \by People complain that types in std:: minus (and analogs) laziness to write. An output two: 1) or to enter something without types, for example, std:: minus2 (a heap of counterparts to old words) 2) or instead of types to write emptiness: std:minus <> () In the second case it is necessary to saw through a template with type by default. The type void here is convenient at once to liquidate possibility to use a term-method of a class std:: minus and to give the chance to use only a static sample method.

5

Re: What for void as default template parameter?

Hello, rg45, you wrote: W>> But void here explicitly is better approaches for a role for at once it is clear that constexpr T operator () (const T& x, const T& y) const it is not meaningful with T == void and even casually has no possibility to be compiled... R> This operator is all the same declared sample, and it  are not connected in any way to sample parameters of the class. I guess that specialization possibility, for something here was put, but scenarios of usage of this possibility for me are shrouded in mystery. Sample operator () at all similar classes of standard library (which functors-wrappers over arithmetical, logical, bit operations and comparing operations represent) meets only in void-specialization. At the general template the operator not sample, and all its parameters (1 in case of unary operations and 2 - in case of binary) have type const T&, where T - template parameter. The sample operator at void-specialization can accept arguments various (by means of forwarding references) and even (in case of binary operations) unequal types that presumes to cause to it more optimized overload of appropriate operation (in that case, of course, if it is) and to avoid creation of superfluous transient objects. Such specialization - a C innovation ++ 14 (transparent operator functors) in which sample overloads of functions-members find, equal_range, count, lower_bound, upper_bound containers-sets and the associative containers also have been entered. These overloads are activated (by means of SFINAE) if the type-member is found out in the comparator used by them (transferred as template argument in the container) is_transparent. At the general templates of comparators of standard library this member misses, and at their void-specializations is available. Value of a default argument void at the general template of classes-funktorov of standard library is necessary only for code abbreviation that instead of std:: less <void> it was possible to write simply std:: less <>. Besides support of support for functions-members of containers, similar specializations can be useful when container type changed (during code modification), and argument of the class-funktora used together with any of algorithms, forgot to change://std:: vector <std:: uint8_t> vector {2, 3, 256};//was std:: vector <std:: uint16_t> vector {2, 3, 256};//became std:: sort (std:: begin (vector), std:: end (vector), std:: greater <std:: uint8_t> {}); At a C stage ++ 11... A C ++ the following construction could be 14 protection against similar modifications: std:: sort (std:: begin (vector), std:: end (vector), std:: greater <decltype (vector):: value_type> {}); Since a C ++ 14, it is possible to write everywhere std:: : sort (std:: begin (vector), std:: end (vector), std:: greater <void> {});... Or it is simple: std:: sort (std:: begin (vector), std:: end (vector), std:: greater <> {}); There is a reasonable question why then such mode is not made by a mode by default (i.e. why std:: less and similar functors of standard library did not lose  a class [saving thus  operator ()]). Probably, business in reverse compatibility, and also aspiration to avoid total  the code.

6

Re: What for void as default template parameter?

Hello, watchmaker, you wrote: W> also that this code, and appropriate specialization std:: minus <> will work generally not. It is any super trick when the code by default does exactly too most, as explicitly registered, but absolutely other function is used thus... Very much, very much cool!

7

Re: What for void as default template parameter?

Hello, B0FEE664, you wrote: BFE> It is any super trick when the code by default does exactly too most, as explicitly registered, but absolutely other function is used thus... Very much, very much cool! Classes of function objects, such as std:: minus and so forth, with rigidly stitched types of parameters and returned value, for today morally became outdated. In the modern variant of the standard they are called precisely "old function adaptors". But to get rid of them suddenly it is impossible, for the clear reasons. But it is possible to give some  - in the form of such here specializations for void. And earlier mandatory nested determinations of types, such as first_argument_type, second_argument_type, result_type, since a C ++ 17, are declared deprecated.