简体   繁体   English

std::to_string 函数的位置

[英]Location of std::to_string function

What is the reason std::to_string function is not a method of std::string class ? std::to_string 函数不是 std::string 类的方法的原因是什么? Because it would be logically more appropriate, isn't it?因为它在逻辑上更合适,不是吗? I want to know why they would not make it:我想知道为什么他们不会成功:

  1. a static member function of std::string class std::string 类的静态成员函数

  2. why would they not integrate it with constructor, so it would be possible to do like std::string ten( 10 ) and to get "10"?为什么他们不将它与构造函数集成,所以可以像 std::string ten( 10 ) 那样做并得到“10”?

C++ is not a OO language. C++ 不是面向对象的语言。 You can write OO code in C++, but you can also not.您可以用 C++ 编写 OO 代码,但也不能。

to_string is not written in an OO style. to_string不是以 OO 风格编写的。

There are advantages;有优势; the largest one is your code can choose to treat to_string as a customization point by doing最大的一个是您的代码可以选择将to_string视为自定义点

using std::to_string;
std::string foo=to_string(x);

and now any type that implements a to_string in its enclosing namespace works with the above code.现在任何在其封闭命名空间中实现to_string类型都可以使用上述代码。

Also, to_string is arguably a method of both source and destination APIs;此外, to_string可以说是源 API 和目标 API 的一种方法; being a method of either is thus questionable.因此,作为任何一种方法都是值得怀疑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM