문제

I install Clang 3.4, and test suffix operator for string literal ""s.

#include <string>

//1. using namespace std; // without compile error.
//2. using std::operator ""s; // or without it compile error, too.
// for success compiles, need 1. or 2. line.

int main()
{
    auto s = "hello world"s; 
}

If I comment 1 and 2 , I get compile error. But I know in big projects 1.-method is bad, and 2.-method is stranger.

QA: Can I use operator ""s without any writing using namespace std and using std::operator ""s ?

도움이 되었습니까?

해결책

Wrapping up the comments:

You have to explicitly pull in those operators with using. This is what is intended by the standard. You should consider to pull in only what you need, either

using namespace std::literals;

or

using namespace std::literals::string_literals;

depending on your needs, the latter being preferable in case of doubt (only pull in what you need, not more). As with all using declarations or directives, you want to limit them to the smallest scope that is practical.

The reason behind the need to pull the operators in explicitly is that the suffixes might be re-used in future extensions in different contexts.

다른 팁

If you found 2nd option strange, that is actually pretty common, you could limit the scope of the "using namespace std" like this:

int main()
{
    using namespace std;
    auto s = "hello world"s; 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top