When and how to use a template literal operator?

2019-03-26 14:16发布

On cppreference there is a mentioning that one can have templated user-literal operators, with some restrictions:

If the literal operator is a template, it must have an empty parameter list and can have only one template parameter, which must be a non-type template parameter pack with element type char, such as

template <char...> double operator "" _x();

So I wrote one like in the code below:

template <char...> 
double operator "" _x()
{
    return .42;
}

int main()
{
    10_x; // empty template list, how to specify non-empty template parameters?
}

Question:

  1. The code works, but how can I use the operator with some non-empty template parameters? 10_x<'a'>; or 10_<'a'>x; does not compile.
  2. Do you have any example of real-world usage of such templated operators?

3条回答
我只想做你的唯一
2楼-- · 2019-03-26 14:28
10_x; // empty template list, how to specify non-empty template parameters?

That isn't quite right. The template parameter list isn't empty. When you write:

template <char... Cs> 
??? operator "" _x()

The Cs get populated from the stuff on the left-hand side of the literal. That is, when you write:

10_x

that calls:

operator ""_x<'1', '0'>();

One simple example would be to build a compile time, overflow-safe binary literal such that:

template <uint64_t V>
constexpr uint64_t make_binary() {
    return V;
}

template <uint64_t V, char C, char... Cs>
constexpr uint64_t make_binary() {
    static_assert(C == '0' || C == '1', "invalid binary");

    return make_binary<2*V + C - '0', Cs...>();
}

template <char... Cs> 
uint64_t operator "" _b()
{
    static_assert(sizeof...(Cs) <= 64, "overflow");

    return make_binary<0, Cs...>();
}

uint64_t a = 101_b; // OK: a == 5
uint64_t b = 102_b; // error: invalid
uint64_t c = 11111111110000000000111111111100000000001111111111000000000011111111110000000000_b; // error: overflow
查看更多
SAY GOODBYE
3楼-- · 2019-03-26 14:36

Your template parameters are already specified--they're the source-code characters comprising your literal value! So for 10_x, you're actually calling:

template<> double operator "" _x<'1', '0'>();

Here's a working example. It compiles without error, and none of the assertions are triggered.

#include <cassert>

enum class MyEnum
{
  ONE,
  TWO,
  THREE
};

template<char...> MyEnum operator "" _e();

template<> MyEnum operator "" _e<'1'>()
{
  return MyEnum::ONE;
}
template<> MyEnum operator "" _e<'2'>()
{
  return MyEnum::TWO;
}
template<> MyEnum operator "" _e<'3'>()
{
  return MyEnum::THREE;
}

int main()
{
  assert(1_e == MyEnum::ONE);
  assert(2_e == MyEnum::TWO);
  assert(3_e == MyEnum::THREE);
}
查看更多
爷的心禁止访问
4楼-- · 2019-03-26 14:37

You can elaborate the parameters pack somehow (as mentioned by others) or access them as a compile-time string if you prefer:

template<int N>
constexpr double f(const char(&str)[N]) { return .42; }

template <char... C> 
constexpr double operator "" _x()
{
     return f({C...});
}

Do you have any example of real-world usage of such templated operators?

You can use the above mentioned technique to deal with compile-time string-to-num converter and have something like 10_x instead of f("10") or whatever.

查看更多
登录 后发表回答