Why does putting std::basic_string
as a parameter of a function template will fail to deduce from const char*
, but it can be deduced successfully when it is constructed directly?
#include <string>
#include <iostream>
template<class Char/*, class Traits, class Allocator*/>
//^doesn't matter whether the second and third template parameter is specified
void printString(std::basic_string<Char/*, Traits, Allocator*/> s)
{
std::cout << s;
}
int main()
{
printString("hello"); //nope
std::basic_string s{ "hello" };//works
}
I found a relevant post here, but the answers didn't explain the reason behind