The following code fails to compile.
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class mc
{
string s;
public:
mc(const std::string s) : s{s}{};
// mc(const char * s) : s{s}{}; // Adding this line would make it work in both cases
};
int main()
{
vector<mc> a = {"Hello", "there"}; //FAILS
vector<mc> b = {mc("Hello"), mc("there")}; //WORKS
}
Depending on compiler error is
no matching function for call to mc::mc(const char&)
note: candidate: ‘mc::mc(std::string)’
note: no known conversion for argument 1 from ‘const char’ to ‘std::string’
or
/usr/include/c++/11/bits/stl_uninitialized.h:138:72: error: static assertion failed: result type must be constructible from value type of input range
Why does this error happen?
There is an implicit constructor from string.
If instead of vector<mc>
they were vector<string>
it would work.