Syf*_*u_H 4 c++ stdstring initializer-list c++11
我有一个模拟窗口的程序;所以我将窗口的内容存储在content一种std::string类型的成员数据中:
class Window {
using type_ui = unsigned int;
public:
Window() = default;
Window(type_ui, type_ui, char);
void print()const;
private:
type_ui width_{};
type_ui height_{};
char fill_{};
std::string content_{};
mutable type_ui time_{};
};
Window::Window(type_ui width, type_ui height, char fill) :
width_{ width }, height_{ height }, fill_{ fill },
content_{ width * height, fill } { // compile-time error here?
//content( width * height, fill ) // works ok
}
void Window::print()const {
while (1) {
time_++;
for (type_ui i{}; i != width_; ++i) {
for (type_ui j{}; j != height_; ++j)
std::cout << fill_;
std::cout << std::endl;
}
_sleep(1000);
std::system("cls");
if (time_ > 10)
return;
}
}
int main(int argc, char* argv[]) {
Window main{ 15, 25, '*' };
main.print();
std::string str{5u, '*'}; // compiles but not OK
std::string str2(5u, '*'); // compiles and OK
cout << str << endl; // ?* (not intended)
cout << str2 << endl; // ***** (ok)
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
正如你可以看到上面我无法初始化成员content与curly-braces-initializer-list该编译器抱怨“缩小型”。但是它可以与“直接初始化”一起使用。
为什么我不能在Constructor-initializer-list中使用上面的Curly-brace-initialization-list来调用std::string(size_t count, char)。
为什么这std::string str{5u, '*'}; // compiles but not OK行得通,却没有预期的结果?
对我来说很重要的是为什么相同的初始化对构造函数成员初始化列表不起作用,但可以起作用main(没有预期的结果)?
首先,由于std::string的构造函数sts::string(size_t count, char)是显式的,因此您不能隐式调用它。
其次,您不是std::string(size_t, char)在调用,content{width * height, fill}而是实际上是在调用std::string(std::initializer_list<char>)。因此,该表达式width * height产生一个无符号的int,然后隐式转换为“相关类型”的char,因此,例如,您通过了Window main{ 15, 25, '*' };哪个操作,产生了(char)15 * 25 = (char)375Undefined Behavior,因为此值溢出a signed char。您可能会在机器上“?” 或其他值作为初始化列表中的第一个元素,但这是未定义的行为,“ ”作为初始化列表中的第二个元素。因此,结果是您传递了std :: initializer_list {'?',' '}。
第二个问题的答案是:“对我来说很重要的是,为什么相同的初始化在构造函数成员初始化列表上不起作用,而在main上起作用(没有预期的结果)?”:
实际上,它与“ Constructor-member-initializer-list”没有任何关系,但实际上请考虑以下问题:
char c{ 127 }; // the maximum integer positive value that a signed char can hold so no narrowing conversion here. So it is OK.
char c2{ 128 }; // Now 128 overflows the variavle c2. c2 is of type char and as you know it can hold positive values in range 0 to 127 and negative -1 to -128
unsigned char uc{ 255 }; // ok because an unsigned char can hold 255
unsigned char uc2{ 300 }; // error as the error above. An unsigned char can hold 255 as max positive value.
unsigned char uc3 = 321; // ok // ok but you may get a warning. a Copy-initialization is not safe.
cout << uc3 << endl; // you may get `A`. 321 % 256 = 65. 65 % 256 = 65. 65 in ASCII in some implementations it represents the character "A".
Run Code Online (Sandbox Code Playgroud)
尽管上面的uc3溢出不是一件好事,但结果是明确的。(溢出未签名的Xtype)。
但是看看这个:
char c3 = 321; // signed char overflows
cout << c3 << endl; // you may get "A" and think it is correct.
Run Code Online (Sandbox Code Playgroud)其上方是未定义的行为。切勿尝试溢出带符号的类型。
constexpr int i = 10;
constexpr int j = 5;
std::string str{ i * j, 'f' }; // ok as long as the values are constexpr and don't cause any narrowing conversion this value: 10 * 5 = 50 which is a well defined signed-char value.
int i = 10;
int j = 5;
std::string str{ i * j, 'f' }; // error. the values of i and j are not constant thus it may overflow a signed char causing narrowing conversion thus the compiler is wise enough to prevent this initialization.
Run Code Online (Sandbox Code Playgroud)