Date: Mon, 03 May 2021 18:35:50 +0100
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?
Regards, Phil.
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?
Regards, Phil.
Received on 2021-05-03 12:35:53