文字运算符模板:为什么不字符串? [英] Literal operator template: why not string?

查看:81
本文介绍了文字运算符模板:为什么不字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再一次,在回答另一个问题时,我忘记了(我的错)仅当找到整数或浮点文字时,才从声明集中选择文字运算符模板。

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.

例如:

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

它可以用作 10_x ,但不能同时用作 foo_x 或 foo _x。

It can be used as 10_x, but it cannot be used neither as foo_x nor as "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?

推荐答案

涉及字符集和编码的复杂性以及其他此类混乱。如果在运行时和编译时字符集编码不同,会发生什么事情;如果有多种方式对同一个字符进行编码,会发生什么事情?使用utf-8编码的字符串与使用不同编码的字符串有什么作用吗?您是否转换为运行时字符集?

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? 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.

但是您已链接到与 N2750 ,并直接说明:

But you linked to a document that linked to N2750, and which directly stated:


可能需要原始订单项。字符串文字的形式,其中

there may be demand for a "raw" form of string literal, in which

"Hello, " L"Worl\u0044!"

有别于

L"Hello, World!"

,但这与翻译阶段相互作用很差,并且尚没有
令人信服的使用案例。

but this interacted badly with phases of translation, and no compelling use cases for this feature were known.

这篇关于文字运算符模板:为什么不字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆