5

This is not about me being lazy to write

auto t = time(nullptr);

instead of hypothetical

auto t = time();

I am mostly interested if this is possible, and if it is(AFAIK it is easily implementable since C++ supports function overloading) why it has not been done.

I know obvious answers like: use <chrono>, nobody wrote proposal, but I wonder if there is a different reason.

My best guess is that nobody wanted to mess with C library functions.

P.S. I understand that some might want to close this question as too vague, but I feel that it is possible to give relatively objective answer to this question.

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • A deeper question is "why does `time_t time(time_t*)` even take that pointer parameter at all?". – Petr Skocik Dec 25 '19 at 20:19
  • Is it worth it? `time(nullptr)` Vs `time()` - who cares? How often do you call that function in any real-life program? Once? Twice? It simply doesn't matter. – Jesper Juhl Dec 25 '19 at 20:19
  • 1
    It's probably inherited from C, which has no function overloading, then kept for compatibility and now probably no one cares anymore, because C++ has [std::chrono](https://en.cppreference.com/w/cpp/header/chrono) now. But it's impossible to say for sure. – Lukas-T Dec 25 '19 at 20:22
  • 1
    C does not allow overloads and C++ did not want std::time from to have different interface from time from since both are valid in C++. – Öö Tiib Dec 25 '19 at 20:23
  • You can simply write a function that calls that function so yes, it can be done. But it is part of the inherited `C` library which the `C++` standard likes to avoid making too many changes to. – Galik Dec 25 '19 at 20:24
  • Here's my (largely speculative) answer about why `time()` both takes a pointer argument and returns a value: https://stackoverflow.com/a/9945669/827263 – Keith Thompson Dec 25 '19 at 20:27
  • 2
    You can fix this yourself: `inline time_t goodtime() {return ::time(nullptr);}` – selbie Dec 25 '19 at 20:34
  • I've updated [the answer](https://stackoverflow.com/a/9945669/827263) I linked to above, confirming my hypothesis by looking at old (early 1970s) UNIX sources. – Keith Thompson Dec 25 '19 at 23:04
  • If you dig into the C library, you'll see other functions that accept a pointer, only to return it (either returning the pointer itself, or a value). The most commonly known example are string functions (`strcpy()`, `strcat()`) which return their first argument to permit chaining of function calls in a single statement e.g. `strcat(a, strcat(b, strcpy(d, "Hello")))` rather forcing writing of three distinct statements. There was considerable advocacy (reflected in some old textbooks) that the single-statement form was "cleaner" than the multi-statement form. – Peter Dec 25 '19 at 23:15

1 Answers1

5

The simple answer is that time(time_t) is “owned” by C rather than by C++: the standard C++ committee doesn’t interfere with the C library unless there are reasons why it is necessary. With C++ you should get a better interface using <chrono> functions.

Also: I don’t think there was a proposal to overload this function. Without a proposal nothing is going to happen and I doubt anybody sufficiently eager to write such a proposal and get it through the process.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • 2
    Also difference between time and std::time would cause lot of confused Stack Overflow posts of noobs who write using namespace std, as first thing in their code. So I'm against such proposal. – Öö Tiib Dec 25 '19 at 20:31
  • 1
    *“owned” by C* This is a non-issue (see std::abs for example). – n. m. could be an AI Dec 25 '19 at 21:02
  • @n.'pronouns'm.: well, the function signature was inherited and nobody felt a need to mess with it. Sure, the C++ committee could choose to overload it but unlike other functions there isn’t any big need. `abs()` was overloaded to avoid accidental conversions and to avoid the need to call the respect `float` or `long double` versions. There aren’t any traps with `time()`. – Dietmar Kühl Dec 25 '19 at 21:05