Subject: [std-proposals] Variadic function template equivalents for operator overloading
From: Phil Endecott (std_proposals_list_at_[hidden])
Date: 2021-05-03 12:35:50
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
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.
STD-PROPOSALS list run by email@example.com
Standard Proposals Archives on Google Groups