candidate template ignored: couldn't infer template argument
I am writing a getline()-like function that can take a predicate, but clang throws
an error, i have little or no idea why it says so
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
template <typename Char, class Traits, class Alloc, class Predicate>
std::basic_istream<Char, Traits>&
getwhile( std::basic_istream<Char, Traits>& stream,
std::basic_string<Char, Traits, Alloc>& str,
Predicate pred )
{
while( Char c = stream.get() ) {
if( !pred( c ) )
break;
str.push_back(c);
}
return stream;
}
// ... blah blah
int main()
{
istringstream getWhileInput( "abcdef$ss" );
std::string input;
getwhile( getWhileInput, input, std::isalpha );
std::cout << "input: " << input << '\n';
}
|
utility_test.cc:15:5: error: no matching function for call to 'getwhile'
getwhile( getWhileInput, input, std::isalpha );
^~~~~~~~
./../utility.hpp:18:1: note: candidate template ignored: couldn't infer template argument 'Predicate'
getwhile( std::basic_istream<Char, Traits>& stream,
|
http://www.cplusplus.com/reference/cctype/isalpha/
http://www.cplusplus.com/reference/locale/isalpha/
¿which one should be used?
getwhile( getWhileInput, input, ( int(*)(int) )std::isalpha );
I'm not quite sure about the cast, you could also specify it with
getwhile( getWhileInput, input, std::ptr_fun<int,int>(std::isalpha) );
By the way, you may simplify the template parameters of your function
1 2 3 4 5
|
template <class Stream, class String, class Predicate>
Stream& getwhile( Stream &stream, String &s, Predicate pred)
{
typedef typename Stream::char_type Char;
//...
|
Last edited on
Topic archived. No new replies allowed.