Liste des Groupes | Revenir à cl c |
bart <bc@freeuk.com> writes:
I am sure it could be done using variadic macros, _Generic, and newer C23 features like "typeof". But it would be difficult to handle non-constant format strings, and may be difficult to do efficiently. (C++ now has something like that in the modern "format" library. Of course, it has the added power and complexity of handling user-defined types too.)This is a C program using one of the extensions from my old compiler:That's nice. Seriously, it's nice. If it were added to a future
>
#include <stdio.h>
#include <time.h>
>
int main(void) {
time_t t = clock();
printf("%v\n", t); # (used to be '?'; changed to 'v')
}
>
The compiler replaces the 'v' format with a conventional format code
according to the type of the expression. For my 'time_t', it happens
to be 'lld'.
edition of the language, I'd likely use it (once I could count on it
being supported, which would take a while).
(You meant "C++" here?)Not particularly. C has programmer-defined operator (and function)It would be very nice if C had some kind of more generic I/O that>
doesn't require remembering arbitrary format strings and qualifiers
for each type, and that doesn't provide format strings for a lot of
types in the standard library, and certainly not for types defined
in user code. And I'd *love* it if a future C standard removed
the undefined behavior for numeric input using *scanf(). Other C
programmers will have different priorities than yours or mine.
If I want to print a time_t value in C++, I just write
`std::cout << t` and the compiler figures out which overloaded
function to call.
That's amazing.
overloading as a language feature. (There are IMHO some serious
flaws in C++'s use of overloaded "<<" for output, but I won't go
into that here.)
[...]
Les messages affichés proviennent d'usenet.