Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't values in initializer_list type-checked?

How does this compile and run without any warnings or errors? I don't understand how the dereferenced value of current, which is an int, can be assigned to the string a without any problem.

class Test {
public:
  string a;

  Test(initializer_list<int> t) {
    auto current = t.begin();

    // I am assigning an int to a string!
    a = *current;
  }
};

int main() {
  Test test{65};
  printf("%s\n", test.a.c_str());
}

The printed string is

A

In contrast, this very similar code produces a compile-time error:

int main() {
  initializer_list<int> test1{65};
  auto current = test1.begin();
  string b = *current;

  return 0;
}

The error is:

error: no viable conversion from 'const int' to 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
  string b = *current;
like image 272
Ludovic Cyril Michel Avatar asked Jul 28 '19 08:07

Ludovic Cyril Michel


1 Answers

Note that a = *current; and string b = *current; perform different things.

a = *current; is an assignment, which leads to the invocation of operator=, and std::string::operator= has an overloading taking char, which makes a = *current; work (after the implicit conversion from int to char).

4) Replaces the contents with character ch as if by assign(std::addressof(ch), 1)

string b = *current; is an initialization, which tries to call the constructor of std::string to initialize b. But these constructors don't have such overloading taking int (or char), then string b = *current; won't work.

like image 94
songyuanyao Avatar answered Nov 10 '22 09:11

songyuanyao