C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] Variadic function template equivalents for operator overloading
From: Ville Voutilainen (ville.voutilainen_at_[hidden])
Date: 2021-05-03 12:50:22


On Mon, 3 May 2021 at 20:36, Phil Endecott via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Dear Experts,
>
> At some point in the early history of C++, someone must have
> thought about how to provide a C++ equivalent of C's printf().
> The issue, of course, is that printf() uses the C varargs
> mechanism, and although that is present in C++ it's not usable
> with most types; and even for the built-in types you only get
> type checking because C compilers know about printf format
> strings.
>
> The solution to this lack of proper C++ varargs was the use
> of operator<< and operator>>, chained together to accept as
> many arguments as necessary.
>
> I have sometimes wondered whether people at the time actually
> considered the use of the operator overloads to be a necessary
> evil, or actually preferable to a variadic function call. Does
> anyone with a long memory know?
>
> Of course we now have variadic function templates so we could
> provide alternatives to operator overloading, if we wished:
>
> strm << a << b << c;
> = put(strm, a,b,c);
>
> Where else does the standard library use operator overloading
> as an alternative to variadic function templates? One example
> is the use of operator| in ranges; that could also be spelled
> pipeline(...). There could also be a concat(...) function
> equivalent to string::operator+.
>
> My impression, anecdotally, is that people prefer the chained
> operator overloading to variadic functions - but perhaps that's
> because they have got used to it, or it's the only thing they
> ever knew. Personally I prefer the regularity that comes from
> using the same fn(args) syntax everywhere and fear subtle errors
> due to operator precedence.
>
> Thoughts anyone?

See
https://en.cppreference.com/w/cpp/utility/format
which was adopted into C++20.


STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups