c++c++11namespacesvisibilityuser-defined-literals

How to make [std::operator""s] visible in a namespace?


#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?


Solution

  • 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?