简体   繁体   English

文字运算符模板:为什么不用字符串?

[英]Literal operator template: why not string?

Once again, while replying to another question, I forgot (my fault) that literal operator templates are picked up from the set of declarations only when integer or floating literals are found. 再一次,在回答另一个问题时,我忘记了(我的错),只有在找到整数或浮点文字时才从声明集中选取文字运算符模板。

As an example: 举个例子:

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

It can be used as 10_x , but it cannot be used neither as foo_x nor as "foo"_x. 它可以用作10_x ,但它既不能用作foo_x也不能用作“foo”_x。

Apart for the obvious reason that is because the standard says that , what's the technical reason (if any) for which they are not considered when dealing with string literals? 除了明显的原因是因为标准说明了 ,在处理字符串文字时,他们不被考虑的技术原因(如果有的话)是什么?

I found also a proposal for that (well, not exactly the same, but it gives an idea), but still that is not an option. 我也找到了一个建议 (好吧,不完全相同,但它提出了一个想法),但仍然不是一个选择。
What prevents them to be used for strings? 什么阻止他们用于字符串?

There where complications involving character sets and encoding and other such mess. 那里的并发症涉及字符集和编码和其他这样的混乱。 What happens if the character set encoding differs between runtime and compile time, what happens if there are multiple ways to encode the same character, does the string being utf-8 encoded vs a different encoding do anything? 如果字符集编码在运行时和编译时间之间有所不同会发生什么,如果有多种方法对同一个字符进行编码会发生什么情况,字符串是utf-8编码还是不同编码做什么? Do you translate to the runtime character set, or not? 您是否转换为运行时字符集?

Maybe there are easy answers; 也许有简单的答案; but there was enough complication that the proposers went "ok, we'll cut this" and the literals got into the standard. 但是有足够的复杂性,提议者“好吧,我们会削减这个”,文字进入标准。

The characters permitted for floating point and integral literals where limited, and did not have this problem. 浮点数和积分文字允许的字符数量有限,并没有这个问题。

But you linked to a document that linked to N2750 , and which directly stated: 但是您链接到与N2750相关联的文档,并直接声明:

there may be demand for a "raw" form of string literal, in which 可能需要一种“原始”形式的字符串文字,其中

 "Hello, " L"Worl\D!" 

is distinguishable from 与...有区别

 L"Hello, World!" 

but this interacted badly with phases of translation, and no compelling use cases for this feature were known. 但是这与翻译阶段的处理非常相似,并且没有引人注目的用例。

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

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