C++流第二个插入运算符

发布于 2024-11-26 06:42:48 字数 175 浏览 4 评论 0原文

是否可以定义第二个插入运算符以具有两种输出类的模式?例如,一个输出所有成员,一个仅输出一些可在日志中进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

亚希 2024-12-03 06:42:48

如果您只想输出 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:

情绪少女 2024-12-03 06:42:48

没有已经定义或按照惯例使用这样的东西。

另外,您无法在 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.

哆兒滾 2024-12-03 06:42:48

不可以。您不能定义自己的运算符(C++ 中不存在 <<<)。但是您可以定义一个返回字符串的 id() 方法并输出它。

No. You can't define your own operators (<<< doesn't exist in C++). But you can define a id() method returning a string and output this.

笔芯 2024-12-03 06:42:48

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.

少钕鈤記 2024-12-03 06:42:48

例如,您可以使用operator |。另一种方法是定义小标记类,并为其重载运算符;示例(非常简单,但你明白了):

template< class T >
struct GrepTag
{
  GrepTag( const T& );
  T value;
}

template< class T >
Greptag< T > MakeGrepTag( const T& x )
{
  return GrepTag< T >( x ); 
}

template< class T >
MyClass& MyClass::operator << ( const GrepTag< T >& g )
{
  //output g.value here
}

MyClass() << MakeGrepTag( "text" );

还有另一种方式,更像标准流,是也使用标签但在内部保留一些状态:

struct GrepTag
{
}

MyClass& MyClass::operator << ( const GrepTag& g )
{
  grepState = true;
}

template< class T >
MyClass& MyClass::operator << ( const T& )
{
  if( grepState )
  {
    //output special
    grepState = false;
  }
  else
  {
    //output normal
  }        
}

MyClass() << GrepTag() << "text";

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):

template< class T >
struct GrepTag
{
  GrepTag( const T& );
  T value;
}

template< class T >
Greptag< T > MakeGrepTag( const T& x )
{
  return GrepTag< T >( x ); 
}

template< class T >
MyClass& MyClass::operator << ( const GrepTag< T >& g )
{
  //output g.value here
}

MyClass() << MakeGrepTag( "text" );

Yet another way, more like the standard streams, is to use a tag as well but keep some state internally:

struct GrepTag
{
}

MyClass& MyClass::operator << ( const GrepTag& g )
{
  grepState = true;
}

template< class T >
MyClass& MyClass::operator << ( const T& )
{
  if( grepState )
  {
    //output special
    grepState = false;
  }
  else
  {
    //output normal
  }        
}

MyClass() << GrepTag() << "text";
归属感 2024-12-03 06:42:48

您无法在 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文