名前空間で user-defined literals を定義する

名前空間で user-defined literals を定義した場合、そのリテラルを使用する時は using、もしくは using namespace をしておく必要があります。

[ソース]

#include <string>
#include <iostream>

namespace hoge{

std::string
operator "" _s(char const* c_str, std::size_t len){
    return { c_str, len };
}

}  // namespace hoge


int
main(){
    // error
//   std::cout << "hello"hoge::_s + "," + "world" << std::endl;

    {
        using hoge::operator "" _s;
        
        std::cout << "hello"_s + "," + "world" << std::endl;
    }


    {
        using namespace hoge;
        
        std::cout << "hello"_s + "," + "world" << std::endl;
    }

    return 0;
}

[出力]

hello,world
hello,world

[コンパイラ]

  • g++ (GCC) 4.8.0 20130210 (experimental)
  • clang++ (LLVM) 3.2 20120514(trunk)