no viable conversion from '__normal_iterator<const char *, [...]>' to '__normal_iterator<pointer, [...]>'

I've made a std::find overload that takes a std::string so i don't have to type
find(begin(str), end(str), c) everytime i use std::find.

However, i don't understand why the compiler generates this error :

1
2
3
4
5
std::string::iterator
find( const std::string& str, std::string::value_type c )
{
    return std::find( std::begin(str), std::end(str), c );
}


error: no viable conversion from '__normal_iterator<const char *, [...]>' to
'__normal_iterator<pointer, [...]>'
return std::find( std::begin(str), std::end(str), c );
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I've also tried this and it compiles fine, but it doesn't seem to work when i use it.

1
2
3
4
5
auto
find( const std::string& str, std::string::value_type c ) -> decltype(std::begin(str))
{
    return std::find( begin(str), end(str), c );
}


EDIT: after i read coder777 's reply, i just realized that this function works, and
found out the error is caused by something else.

I just don't understand, isn't the template argument for std::find<> will be std::string::iterator ?
Last edited on
The point is that you pass const std::string. The result will be std::string::const_iterator
*Sigh* i'm an idiot.

Many thanks !!
Topic archived. No new replies allowed.