哪些用户定义的文字是由标准预定义的?
我的问题听起来像是一个矛盾,但我不知道除了 user-defined-literal
之外还能如何引用新的文字语法。
std::string operator "" s ( const char* str, size_t len )
{
return std::string( str, len );
}
assert( "foo"s == "bar"s );
我记得听说用户定义的文字应该以 _
前缀开头。这意味着该库为我们定义了一些无前缀文字。
标准库中是否提供了一些 UDL?
如果有,它们是什么?
My question sounds like a contradiction, but I don't know how else to refer to the new literal syntax other than user-defined-literal
.
std::string operator "" s ( const char* str, size_t len )
{
return std::string( str, len );
}
assert( "foo"s == "bar"s );
I remember hearing that user defined literals should start with an _
prefix. That would imply that the library defines some non-prefixed literals for us.
Does the standard provide some UDLs in the the standard library?
If yes, what are they?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
标准库实际上定义了没有用户定义的文字。我们也许期望复数,但事实并非如此。
另一方面,也有建议再次删除它们
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3250.html
所以我们还不知道会发生什么。
The standard library actually defines no user defined literals. We would perhaps have expected complex numbers, but no.
On the other hand, there is also a proposal to remove them again
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3250.html
so we don't yet know what happens.
该语言已经使用常规文字后缀,例如
1U
。如果您使用
U
作为用户定义的文字,那么它会变得不明确,因此是推荐的。整数后缀:
u
、U
、l
、L
、ll
、LL
浮动后缀:
f
、F
、l
,L
The language already use regular literals suffixes, for example
1U
.It would become ambiguous if you were to use
U
as a user-defined-literal, thus the recommendation.integer-suffix:
u
,U
,l
,L
,ll
,LL
floating-suffix:
f
,F
,l
,L
与@Matthieu的答案相反, FIDS< /a> 在 2.14.8 [lex.ext] p1 下表示以下内容:
因此,即使您定义了这些文字,也会采用预定义的文字,并且不会产生歧义。
In contrast to @Matthieu's answer, the FIDS says the following under 2.14.8 [lex.ext] p1:
So even if you define those literals, the predefined ones are taken and there is no ambiguity.
没有标准库组件对用户定义的文字提出声明。 @Bo 提到复杂是一种可能性。
另一个是 bitset:
我预测在即将到来的 TR2 库增强功能中,将会有针对各种库组件的文字运算符的建议。
我预计后缀命名空间会发生一些冲突。您可以在命名空间中声明文字运算符并防止出现多个不明确的定义,但您无法在实际文字中按命名空间区分实际后缀。我们拭目以待。
No standard library components have staked a claim on a user-defined literal. @Bo mentioned complex as a possibility.
Another is bitset:
I predict there will be proposals for literal operators for various library components in the upcoming library TR2 enhancements.
I anticipate some collisions over the suffix namespace. You can declare literal operators in a namespace and prevent multiple ambiguous definitions but you can't distinguish the actual suffixes by namespace in an actual literal. We'll see.