是否可以使用 cout 从用户定义类型自动转换为 std::string ?

发布于 2024-09-29 04:06:10 字数 435 浏览 4 评论 0原文

正如问题中所示,如果我在类中定义一个字符串运算符:

class Literal {
  operator string const () {
    return toStr ();
  };

  string toStr () const;
};

然后使用它:

Literal l1 ("fa-2bd2bc3e0");
cout << (string)l1 << " Declared" << endl;

通过显式强制转换一切都会正常,但是如果我删除 (string) 编译器会说它需要在 std 中声明的强制转换运算符: :细绳。它不应该自动转换我的类型吗? 已解决:我正在重载运算符<< (ostream&os, const Literal&l)。

As in the question, if I define a string operator in my class:

class Literal {
  operator string const () {
    return toStr ();
  };

  string toStr () const;
};

and then I use it:

Literal l1 ("fa-2bd2bc3e0");
cout << (string)l1 << " Declared" << endl;

with an explicit cast everything goes right, but if I remove the (string) the compiler says that it needs a cast operator declared in std::string. Shouldn't it cast my type automatically?
SOLVED: I'm overloading operator<< (ostream& os, const Literal& l).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

以酷 2024-10-06 04:06:10

不.. std::string 必须有一个以 Literal 作为参数的构造函数。

你可以做的是重载运算符 <<对于你的 Literal 类,让它强制转换并插入到那里的流中。

ostream &operator <<(std::ostream &stream, const Literal &rhs)
{
    stream << (string) rhs;
    return stream;
}

No.. std::string would have to have a constructor that took Literal as an argument.

What you could do is overload operator << for your Literal class and have it cast and insert into the stream in there.

ostream &operator <<(std::ostream &stream, const Literal &rhs)
{
    stream << (string) rhs;
    return stream;
}
提笔落墨 2024-10-06 04:06:10

简短回答:继续使用强制转换或 toStr(),或者编写您自己的运算符<< 函数。 (我更喜欢 l1.toStr() 而不是 (string)l1。)

长答案:
如果标准库有一个

std::ostream& operator<<( std::ostream&, std::string const& );

几乎可以实现的功能,那么这可能会起作用,但从技术上讲却不行。 ostreamstring 都是模板实例化的真正类型定义。还有一个模板函数可以将一个插入另一个。

// This is somewhat simplified.  For the real definitions, see the Standard
// and/or your complying implementation's headers.
namespace std {
  typedef basic_string<char> string;
  typedef basic_ostream<char> ostream;

  template <typename CharT>
  basic_ostream<CharT>& operator<<(
    basic_ostream<CharT>&, 
    basic_string<CharT> const&);
}

因此,当您使用 cout << str 其中 str 的类型是 std::string,它可以找出使用该模板函数,其中 CharT = char >。

但 C++ 不允许编译器找出隐式类型转换(Literalstring)并推导模板函数模板参数(CharT = char)在同一个调用中。

Short answer: Keep using a cast or toStr(), or write your own operator<< function. (I would prefer l1.toStr() to (string)l1.)

Long answer:
This might work if the Standard Library had a function

std::ostream& operator<<( std::ostream&, std::string const& );

Which it almost does, but not technically. Both ostream and string are really typedefs of template instantiations. And there's a template function for inserting one into the other.

// This is somewhat simplified.  For the real definitions, see the Standard
// and/or your complying implementation's headers.
namespace std {
  typedef basic_string<char> string;
  typedef basic_ostream<char> ostream;

  template <typename CharT>
  basic_ostream<CharT>& operator<<(
    basic_ostream<CharT>&, 
    basic_string<CharT> const&);
}

So when you use cout << str where the type of str is std::string, it can figure out to use that template function, with CharT = char.

But C++ doesn't allow you to have the compiler figure out both an implicit type conversion (Literal to string) and deduce template function template parameters (CharT = char) on the same call.

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