-3

This example uses the date library without any using namespace:

#include <iostream>
#include <date/date.h>
//using namespace std;
//using namespace date;
int main() {
    date::year_month_day startDate  = 2018_y / 1 / 6;
    std::cout << startDate << '\n';
    return 0;
}

but does not compile, error: unable to find numeric literal operator 'operator""_y'

How to use this library without using namespace?

UPDATE:

I changed the code as follows, but there are still many errors.

#include <iostream>
#include <date/date.h>
int main() {
    date::sys_time<std::chrono::nanoseconds> tp;
    std::istringstream in1{"2018-12-21 01:15:31"};
    in1 >> date::parse("%F %T", td);
    std::cout << tp << '\n';
    return 0;
}

error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream'} and 'date::sys_time ...

sadig
  • 431
  • 1
  • 4
  • 9
  • 2
    From the page you linked: *"The entire library is in namespace date. The examples in this overview assume: using namespace date; using namespace std::chrono; in order to cut down on the verbosity."* – Blaze Mar 11 '19 at 08:17
  • 2
    If you check [where `_y` is defined in the header file](https://github.com/HowardHinnant/date/blob/master/include/date/date.h#L939) you will see that it's defined in sub-namespace `literals`. Which means you could do `using namespace date::literals;` to get only the literals while not getting the rest of the `date` namespace. – Some programmer dude Mar 11 '19 at 08:27
  • Please check the question again. – sadig Mar 11 '19 at 16:23

2 Answers2

1

Numeric literal operator 'operator""_y' is declared inside of namespace 'date'.

You can use 'using namespace date' or 'using namespace date::literals'

More information: How to refer to user defined literal operator inside a namespace?

1
#include <iostream>
#include <date/date.h>

int
main()
{
    using namespace date::literals;
    date::year_month_day startDate  = 2018_y / 1 / 6;
    std::cout << startDate << '\n';
}

And:

#include <iostream>
#include <date/date.h>

int
main()
{
    date::sys_time<std::chrono::nanoseconds> tp;
    std::istringstream in1{"2018-12-21 01:15:31"};
    in1 >> date::parse("%F %T", tp);
    using date::operator<<;
    std::cout << tp << '\n';
}
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577