13
#include <chrono>

namespace X
{
using namespace std;
struct A
{
    std::chrono::seconds d = 0s; // ok
};
}

namespace Y
{
struct B
{
    std::chrono::seconds d = 0s; // error
};
}

The error message is:

error : no matching literal operator for call to 'operator""s' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template std::chrono::seconds d = 0s;

My question is:

I don't want to use namespace std; in namespace Y; then, how should I make std::operator""s visible in namespace Y?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

16

If you want to have all the chrono literals then you can use

using namespace std::chrono_literals;

If you just want operator""s then you can use

using std::chrono_literals::operator""s;

Do note that at least on coliru gcc issues a warning for the above line but clang does not. To me there should be no warning. I have asked a follow up question about this at Should a using command issue a warning when using a reserved identifier?

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I think you need `using namespace ...;` for the first, the second one issue a warning on g++. – Holt Jan 03 '17 at 13:10
  • @Holt I think that is a gcc bug. Yes it is a reserved name but we are not defining it, just using it so it should be okay. I just added a note about that and that clang does not warn. – NathanOliver Jan 03 '17 at 13:11
  • This is meant to be used as `using namespace std::chrono_literals;`. The `literals::` part is redundant. – T.C. Jan 03 '17 at 18:50
  • @T.C. Good point. I forgot you could drop the `literal` namespace. – NathanOliver Jan 03 '17 at 18:54
3

tl;dr: Use

using namespace std::string_literals

These operators are declared in the namespace std::literals::string_literals, where both literals and string_literals are inline namespaces. Access to these operators can be gained with using namespace std::literals, using namespace std::string_literals, and using namespace std::literals::string_literals.

Source: https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

Marin Shalamanov
  • 736
  • 6
  • 17