This is the C++ program:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int test_string(const string & str) {
return str.size();
}
void main() {
test_string(""); //can compile
vector<string> v;
string sum = accumulate(v.cbegin(), v.cend(), ""); //cannot compile
}
I want to use implicit conversion from const char *
to string
in the call of generic STL function accumulate
. I know that conversion from const char *
to string is not explicit, so we can pass const char *
parameter to calls in which a string
type is required. This can be proved by the above test_string
function. But when I did the same thing in accumulate
, the compiler complain:
error C2440: '=': cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'const char *'
The code works only when I replaced ""
with string("")
. I don't understand why the implicit conversion works for my custom function but does not work in accumulate
. Can you explain that? Thanks a lot.
PS: I am using Visual Studio 2015.