C++ Stringstream 初始化
C++ Stringstream initializations
我有两个代码片段:
这不编译:
std::string reverseSentence(std::string sentence) {
std::stringstream stream = sentence;
}
这样做:
std::stringstream stream (sentence);
我的理解是 T foo = expr
是 T foo(expr)
。因此,这两个 stringstream 初始化不是等价的吗?为什么一个编译,另一个不编译?
constructor of std::basic_stringstream
taking std::string
is marked as explicit
, it's not considered in copy initalization喜欢std::stringstream stream = sentence;
。
std::stringstream stream (sentence);
是 direct initialization,它也考虑了 explicit
构造函数。
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and non-explicit user-defined conversion functions, while direct-initialization considers all constructors and all user-defined conversion functions.
我有两个代码片段:
这不编译:
std::string reverseSentence(std::string sentence) {
std::stringstream stream = sentence;
}
这样做:
std::stringstream stream (sentence);
我的理解是 T foo = expr
是 T foo(expr)
。因此,这两个 stringstream 初始化不是等价的吗?为什么一个编译,另一个不编译?
constructor of std::basic_stringstream
taking std::string
is marked as explicit
, it's not considered in copy initalization喜欢std::stringstream stream = sentence;
。
std::stringstream stream (sentence);
是 direct initialization,它也考虑了 explicit
构造函数。
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and non-explicit user-defined conversion functions, while direct-initialization considers all constructors and all user-defined conversion functions.