用户定义的文字

User-defined literals

在 cppreference.com 上的 "User-defined literals" 中,这是什么意思?

b) otherwise, the overload set must include either, but not both, a raw literal operator or a numeric literal operator template. If the overload set includes a raw literal operator, the user-defined literal expression is treated as a function call operator "" X("n")

拜托,我需要一个简单的例子来说明这段文字。


unsigned long long operator "" _w(unsigned long long);
unsigned operator "" _u(const char*);

int main() {
    12_w; // calls operator "" _w(12ULL)
    12_u; // calls operator "" _u("12")
}

根据您 link 中的示例稍作更改。

这里 12_w 调用 operator "" _w(12ULL) 因为有 一个参数类型为 unsigned long long 的文字运算符,而 12_u 调用operator "" _u("12") 因为只有 一个原始文字运算符