如何使 [std::operator""s] 在命名空间中可见?
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
};
}
错误信息是:
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;
我的问题是:
我不想 use namespace std;
在 namespace Y
;那么,我应该如何使 std::operator""s
在 namespace Y
中可见?
如果你想拥有所有的计时文字,那么你可以使用
using namespace std::chrono_literals;
如果你只想operator""s
那么你可以使用
using std::chrono_literals::operator""s;
请注意至少在 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?
tl;dr:使用
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
.
来源:https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s
#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
};
}
错误信息是:
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;
我的问题是:
我不想 use namespace std;
在 namespace Y
;那么,我应该如何使 std::operator""s
在 namespace Y
中可见?
如果你想拥有所有的计时文字,那么你可以使用
using namespace std::chrono_literals;
如果你只想operator""s
那么你可以使用
using std::chrono_literals::operator""s;
请注意至少在 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?
tl;dr:使用
using namespace std::string_literals
These operators are declared in the namespace
std::literals::string_literals
, where bothliterals
andstring_literals
are inline namespaces. Access to these operators can be gained withusing namespace std::literals
,using namespace std::string_literals
, andusing namespace std::literals::string_literals
.
来源:https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s