2 분 소요

개요

  • 표준 리터럴
    • 종류 접미사 의미 버전
      string_literals s string C++14
      string_view_literals sv string_view C++17
      complex_literals i complex C++14
        if complex C++14
        il complex C++14
      chrono_literals y chrono::year C++20
        d chrono::day C++20
        h chrono::hours C++14
          chrono::duration<long int, std::ratio<3600> > C++14
        min chrono::minutes C++14
          chrono::duration<long int, std::ratio<60> > C++14
        s chrono::seconds C++14
          chrono::duration C++14
        ms chrono::milliseconds C++14
          chrono::duration<long int, std::ratio<1, 1000> > C++14
        us chrono::microseconds C++14
          chrono::duration<long int, std::ratio<1, 1000000> > C++14
        ns chrono::nanoseconds C++14
          chrono::duration<long int, std::ratio<1, 1000000000> > C++14
  • 사용자 정의 리터럴
    • _를 이용
    • 사용 가능한 파라미터 - 파라미터 버전
      const char _ C++11  
      unsigned long long int C++11  
      long double C++11  
      char C++11  
      wchar_t C++11  
      char8_t C++20  
      char16_t C++11  
      char32_t C++11  
      const char _ , std::size_t C++11  
      const wchar*t * , std::size_t C++11  
      const char8*t * , std::size_t C++20  
      const char16*t * , std::size_t C++11  
      const char32*t * , std::size_t C++11  


예제

  • 코드
    #include <chrono>
    #include <complex>
    #include <cstring>
    #include <iostream>
    #include <string>

    using namespace std;

    class Test {
    	public:
    		Test(int i) { cout << "Test(int i)" << endl; }
    		Test(string s) { cout << "Test(string s)" << endl; }

    		void func(){};
    };

    Test operator""_t(unsigned long long int l) {
    	return Test(static_cast<int>(l));
    }

    Test operator""_t(const char *c, size_t t) {
    	cout << t << endl;
    	return Test(c);
    }

    template <typename T> constexpr string type_name() {
    	const string s = __PRETTY_FUNCTION__;
    	const int prefixSize = s.find("[with T = ") + strlen("[with T = ");

    	return string(s.data() + prefixSize, s.find(';') - prefixSize);
    }

    void user_defined_literals_test() {
    	Test t1 = 1_t;
    	t1.func();

    	Test t2 = "abc"_t;
    	t2.func();
    }

    void string_literals_test() {
    	using namespace string_literals;

    	cout << type_name<decltype("a")>() << endl;
    	cout << type_name<decltype("a"s)>() << endl;
    }

    void string_view_literals_test() {
    	using namespace string_view_literals;

    	cout << type_name<decltype("a"sv)>() << endl;
    }

    void complex_literals_test() {
    	using namespace complex_literals;

    	cout << type_name<decltype(1.1i)>() << endl;
    	cout << type_name<decltype(1.1if)>() << endl;
    	cout << type_name<decltype(1.1il)>() << endl;
    }

    void chrono_literals_test() {
    	using namespace chrono_literals;

    	cout << type_name<decltype(1y)>() << endl;
    	cout << type_name<decltype(1d)>() << endl;
    	cout << type_name<decltype(1h)>() << endl;
    	cout << type_name<decltype(1min)>() << endl;
    	cout << type_name<decltype(1s)>() << endl;
    	cout << type_name<decltype(1ms)>() << endl;
    	cout << type_name<decltype(1us)>() << endl;
    	cout << type_name<decltype(1ns)>() << endl;
    }

    int main() {
    	user_defined_literals_test();

    	cout << "------ 1" << endl;

    	string_literals_test();

    	cout << "------ 2" << endl;

    	string_view_literals_test();

    	cout << "------ 3" << endl;

    	complex_literals_test();

    	cout << "------ 4" << endl;

    	chrono_literals_test();

    	return 0;
    }
  • 실행 결과
    Test(int i)
    3
    Test(string s)
    ------ 1
    const char (&)[2]
    std::__cxx11::basic_string<char>
    ------ 2
    std::basic_string_view<char>
    ------ 3
    std::complex<double>
    std::complex<float>
    std::complex<long double>
    ------ 4
    std::chrono::year
    std::chrono::day
    std::chrono::duration<long int, std::ratio<3600> >
    std::chrono::duration<long int, std::ratio<60> >
    std::chrono::duration<long int>
    std::chrono::duration<long int, std::ratio<1, 1000> >
    std::chrono::duration<long int, std::ratio<1, 1000000> >
    std::chrono::duration<long int, std::ratio<1, 1000000000> >