C++流第二个插入运算符
是否可以定义第二个插入运算符以具有两种输出类的模式?例如,一个输出所有成员,一个仅输出一些可在日志中进行 grep 的基本唯一标识符?如果有,是否有通常选择的运营商?我猜想与 <<
类比,如果这是合法的,人们可能会使用 <<<
吗?
谢谢
Is it possible to define a second insertion operator to have two modes of outputting a class? Say e.g. one that outputs all members and one that just outputs some basic unique identifier that is grep-able in a log? If so, is there an operator that is usually chosen? I would guess as analogy to <<
one might use <<<
if that is legal?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只想输出 id,那么最好的想法可能是提供一种方法来获取可流式传输的类型中的 id(例如
std::string id() const;
)。对于其他处理代码的人来说,这比一些奇怪的运算符使用更直观。您对
<<<
的建议(不可能在 C++ 中创建新的运算符,但暂时忽略它)表明您很高兴在此时有不同的代码称呼。因此,您获得的唯一好处就是节省了一些角色的源代码;这不值得混淆。相比之下,在某些情况下,您希望使用相同的流表示法来调用不同的行为,例如在仅 id 数据和完整数据之间切换,或不同的表示形式(例如标签/值、CSV、XML 和二进制)。这些替代方案通常最好通过以下任一方式进行传达:
If you want to output only the id, then the best idea is probably to provide a method to get the id in a type that's streamable (e.g.
std::string id() const;
). That's much more intuitive to other people working on the code than some strange operator use.Your suggestion of
<<<
(it's not possible to create new operators in C++, but ignoring that for a moment) reveals that you're happy for there to be different code at the point of call. Therefore, the only benefit you'd get would be the saving of a few character's source code; it isn't worth the obfuscation.By way of contrast, there are situations where you want the same streaming notation to invoke different behaviours, such as switching between id-only and full data, or different representations such as tag/value, CSV, XML, and binary. These alternatives are usually best communicated by either:
std::ostream
), and definingXMLStream& operator<<(XMLStream&, const My_Type&)
etc, and/or没有已经定义或按照惯例使用这样的东西。
另外,您无法在 C++ 中定义自己的运算符,您必须使用该语言中已有的且可重载的运算符之一,并且
<<<
不是一个C++ 中的运算符,所以无论如何它都已经过时了。我强烈建议您不要使用其他运算符来执行此操作。 (请参阅规则#1此处以获取更全面的说明。)如果输出之间存在细微差别操作时,精心选择的函数名称比任意选择不明确的运算符对于编写更好的代码大有帮助。
There's no such thing already defined or in use by convention.
Also, you cannot define your own operators in C++, you have to use one of the ones already in the language and overloadable, and
<<<
isn't an operator in C++, so it is out anyway.I'd strongly recommend you don't use some other operator for this. (See rule #1 here for a more thorough explanation.) If you have subtle differences between output operations, well-chosen functions names go a long way for making better code than unclear operators arbitrarily picked.
不可以。您不能定义自己的运算符(C++ 中不存在
<<<
)。但是您可以定义一个返回字符串的 id() 方法并输出它。No. You can't define your own operators (
<<<
doesn't exist in C++). But you can define aid()
method returning a string and output this.C++中没有
<<<
这样的运算符。但是,您可以自由地实现,例如
operator <(ostream&,Object&)
,它可以执行您想要的操作。问题是,当您尝试将<
和<<
链接在一起时,代码可能会变得不可读。There is no such operator as
<<<
in C++.You are, however, free to implement, for example
operator <(ostream&,Object&)
, which would do what you want. The problem is, code may get unreadable when you try to chain<
and<<
together.例如,您可以使用
operator |
。另一种方法是定义小标记类,并为其重载运算符;示例(非常简单,但你明白了):还有另一种方式,更像标准流,是也使用标签但在内部保留一些状态:
you can use
operator |
for instance. Another way of doing this is to define small tag classes for which the operator is overloaded; example (pretty simplistic but you get the point):Yet another way, more like the standard streams, is to use a tag as well but keep some state internally:
您无法在 C++ 中定义自己的运算符。您只能重载现有的那些。
因此,我建议不要使用运算符在日志中输出可 grep 的基本唯一标识符。这与任何现有的操作员角色都不对应。请改用一个方法,例如
exportToLog()
。You cannot define your own operators in C++. You can only overload those that exist.
So I recomend not using an operator for outputting basic unique identifier grep-able in a log. This doesn't correspond to any existing operator role. Use a method instead, such as
exportToLog()
.