std::stoi
function takes std::string
as parameter. I suppose the question arises why the following code works but not yours.
std::string s = "1";
int i = stoi(s);
The above code compiles fine without std::
prefix because of argument-dependent lookup (ADL).
Basically, if a parameter is in a namespace, function resolution will be performed in that namespace as well.
In this case, string
is in std
namespace.
In your case, however, the provided parameter is a char
reference.
Since no parameter is in a namespace, function resolution occurs only on global scope, thus it fails.
Function resolution succeeds when std::stoi
is explicitly provided.
In that case, an std::string
is implicitly constructed from the char
reference, and given as the first parameter to stoi
.
Note that being able to avoid typing the namespace of a function with the help of ADL does not necessarily mean you should do that way.
Explicitly typing the namespace prevents such issues.
If nothing else, it informs the future reader about the origins of a function.