Wiem, że mogę użyć tablicy znaków i listy inicjalizacji, aby wypełnić ciąg znaków.Ciąg C++ - dziwne zachowanie podczas korzystania z konstruktora listy inicjowania
Wygląda na to, że kompilator dokonuje niejawnej promocji z int na initializer_list lub allocator. Ale nie wiem, dlaczego nie daje mi ono żadnego ostrzeżenia i dlaczego to powoduje, że jest ukryty.
Czy możesz mi wyjaśnić, co dzieje się z ciągami s4 i s5?
#include <iostream>
#include <string>
using namespace std;
class A{
};
int main() {
// string::string(charT const* s)
string s1("12345");
// 5 - because constructor takes into account null-terminated character
cout << s1.size() << endl;
// string(std::initializer_list<charT> ilist)
string s2({'1','2','3','4','5'});
// 5 - because string is built from the contents of the initializer list init.
cout << s2.size()<<endl;
// string::string(charT const* s, size_type count)
string s3("12345",3);
// 3 - Constructs the string with the first count characters of character string pointed to by s
cout << s3.size() << endl;
// basic_string(std::initializer_list<CharT> init,const Allocator& alloc = Allocator()); - ?
string s4({'1','2','3','4','5'},3);
// 2 - why this compiles (with no warning) and what this result means?
cout << s4.size() << endl;
string s5({'1','2','3','4','5'},5);
// 0 - why this compiles (with no warning) and what this result means?
cout << s5.size() << endl;
// basic_string(std::initializer_list<CharT> init,const Allocator& alloc = Allocator());
// doesn't compile, no known conversion for argument 2 from 'A' to 'const std::allocator<char>&'
//string s6({'1','2','3','4','5'},A());
//cout << s6.size() << endl;
return 0;
}
Czy możesz mi wytłumaczyć, czego można się spodziewać zamiast tego, co się dzieje? Nie widzę niczego nie zamierzonego w tym kodzie. – dhein
Tak, oczywiście. Spodziewałem się takiego samego zachowania dla obu konstruktorów, także biorąc pod uwagę tablicę znaków i listy inicjalizatorów. – tomekpe