Git Product home page Git Product logo

Comments (2)

HowardHinnant avatar HowardHinnant commented on July 17, 2024

Thanks for the nice words.

My rationale to-date for not having them (for example for year_month_day) is:

  • The default constructor would not be higher performance than the {y, m, d} constructor.
  • It is difficult to choose a value for the default constructed object that is not surprising.
  • It is easy to supply a "default value" of your own choosing for different situations.

For an example of the latter:

#include "date.h"
#include <iostream>
#include <fstream>

void
input(std::istream& is, date::year_month_day& x)
{
    int y, m, d;
    is >> y >> m >> d;
    // Don't modify x if the read failed
    if (!is.fail())
        x = date::year{y}/m/d;
}

int
main()
{
    std::ifstream infile("test.dat");
    while (infile)
    {
        using namespace date::literals;
        auto a_date = jan/0/0;  // initialize to !ok()
        input(infile, a_date);
        if (a_date.ok())
            std::cout << a_date << '\n';
        else
            break;
    }
}

In this example I want to "default" the date to something that is !ok(). If the de-serialization works, it should set it to something that is ok().

I have not yet come across a compelling use case where I really do need default constructors. And newing arrays of things is not compelling in modern C++ (use vector instead).

I am open to being shown compelling use cases for default constructors for these types: Use cases that demonstrate advantages that will outweigh the disadvantage of having a surprising default value no matter what gets chosen.

from date.

jkhoogland avatar jkhoogland commented on July 17, 2024

Thanks for the elaborate clarification.
Fair enough, I will try to make up some examples.

from date.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.