简体   繁体   English

什么是C ++ 20的字符串文字运算符模板?

[英]What is C++20's string literal operator template?

What is C++20's string literal operator template? 什么是C ++ 20的字符串文字运算符模板? Cppreference's example in this respect is quite concise and not very clear to me: Cppreference在这方面的例子非常简洁,对我来说不是很清楚:

struct A { A(const char *); auto operator<=>(const A&) const = default; };

template<A a> A operator ""_a(); 

In trying to understand what this feature is I've just learned that you can have numeric literal operator templates in C++, which make each digit of numerical constant be passed as a non-type argument to a template (cf. a better explanation here ). 在试图理解这个特性是什么时,我刚学会了你可以在C ++中使用数字文字运算符模板 ,这使得数字常量的每个数字都作为非类型参数传递给模板(参见这里更好的解释) 。 Currently, literal operator templates do not work with character literals, though there are compilers extensions enabling that. 目前,文字运算符模板不支持字符文字,尽管有编译器扩展可以实现。 I don't think C++20's string literal operator templates have anything to do with that as I've learned that proposals to extend literal operator templates to work with character literals were voted down in the commitee? 我不认为C ++ 20的字符串 文字运算符模板与此有关,因为我已经了解到扩展文字运算符模板以处理字符文字的提议在委员会中被否决了?

There were two separate proposals: 有两个单独的提案:

  • Allowing string literals as non-type template parameters ( P0424 ) 允许字符串文字作为非类型模板参数( P0424
  • Allowing class types as non-type template parameters ( P0732 ) 允许类类型作为非类型模板参数( P0732

The first proposal was partially merged into the second. 第一个提案部分合并到第二个提案。 String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. 字符串文字仍然不是有效的参数作为非类型模板参数,但它们是类类型的有效参数。 The example from [temp.arg.nontype]/4 might help: [temp.arg.nontype] / 4中的示例可能会有所帮助:

 template<class T, T p> class X { /* ... */ }; X<const char*, "Studebaker"> x; // error: string literal as template-argument const char p[] = "Vivisectionist"; X<const char*, p> y; // OK struct A { constexpr A(const char*) {} friend auto operator<=>(const A&, const A&) = default; }; X<A, "Pyrophoricity"> z; // OK, string literal is a constructor argument to A 

However, the part of the first proposal which extended the literal operators was what was merged into the second, [lex.ext]/5 : 但是,扩展文字运算符的第一个提案的部分是合并到第二个, [lex.ext] / 5

If S contains a literal operator template with a non-type template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the form operator "" X<str>() 如果S包含带有非类型模板参数的文字运算符模板,其中str是格式正确的模板参数,则文字L被视为表单operator "" X<str>()的调用

So using this: 所以用这个:

struct A { A(const char *); auto operator<=>(const A&) const = default; };     
template<A a> A operator ""_a() { return a; }

We can write "Hello"_a , which will be interpreted as calling operator "" _a<A("Hello")> . 我们可以编写"Hello"_a ,它将被解释为调用operator "" _a<A("Hello")>


Note that these rules are slightly in flux, as the defaulted <=> requirement will be changing to a defaulted == requirement as per P1185 . 请注意,这些规则略有不同,因为默认的<=>要求将根据P1185更改为默认==要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM