Template argument deduction cannot be performed

G

George

Dear All,

I'm compiling the code below with IBM's xlC 6.0 and get the message,

"rmspace.cpp", line 34.48: 1540-0298 (S) Template argument deduction
cannot be performed using the function "template bool
space_pred(basic_string<T,char_traits<T>,allocator<T> >::value_type)".

If I use gcc4.0..2, I get,

rmspace.cpp:34: error: no matching function for call to
'remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'

If I specify space_pred<char> in line 34 instead, gcc compiles. xlC
still does not.

In theory, according to the C++ standard, shouldn't the compiler be
able to deduce the template argument?

(Oh, and I welcome any comments to a better way to remove redundant
spaces....)

-------------------------<coup>---------------------------

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

template <typename T>
bool space_pred(typename basic_string<T>::value_type const & vt)
{
static bool space_found;
const ctype<T>& ct = use_facet<ctype<T> >(locale());
if (ct.is(ctype<T>::space, vt)) {
if (space_found) {
return true;
} else {
space_found = true;
return false;
}
} else {
space_found = false;
return false;
}
}

int main()
{
string gs = "this is a string
with various \
spaces \n and words ";

cout << gs << endl;
// xlc always give the error,
// Template argument deduction cannot be performed for function
'bool space_pred'...
// gcc 4.0.2 will work
gs.erase(remove_if(gs.begin(),gs.end(),space_pred),gs.end());
cout << gs << endl;
return 0;
}
~


~


~
 
V

Victor Bazarov

George said:
I'm compiling the code below with IBM's xlC 6.0 and get the message,

"rmspace.cpp", line 34.48: 1540-0298 (S) Template argument deduction
cannot be performed using the function "template bool
space_pred(basic_string<T,char_traits<T>,allocator<T> >::value_type)".

If I use gcc4.0..2, I get,

rmspace.cpp:34: error: no matching function for call to
'remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'

If I specify space_pred<char> in line 34 instead, gcc compiles. xlC
still does not.

Is there a different message that xlC gives in this case?
In theory, according to the C++ standard, shouldn't the compiler be
able to deduce the template argument?
No.

[..]

V
 
G

George

Is there a different message that xlC gives in this case?

No.

Oh, I understand then. So deduction of template arguments is just a
gratuity. (aka icing on the cake; not guaranteed nor obligatory
feature; if your compiler gives it, you will be convenienced but don't
expect it all the time, if you don't like it go and buy another
compiler of the thousands available on the market or download gcc
sources the add the feature yourself....)

Did I get the right Victor B.?

V
 
V

Victor Bazarov

George said:
[..] So deduction of template arguments is just a
gratuity. (aka icing on the cake; not guaranteed nor obligatory
feature; if your compiler gives it, you will be convenienced but don't
expect it all the time, if you don't like it go and buy another
compiler of the thousands available on the market or download gcc
sources the add the feature yourself....)

Did I get the right Victor B.?

Um... Nnnnnno. Not exactly. The Standard says that there are certain
contexts in which the template argument (template, type, or non-type)
_can_ be deduced, which makes me think that in those contexts the ability
of the compiler to deduce the arguments is *mandated*, i.e. not the "icing
on the cake" as you put it. Your context is just not one of those named
in the Standard.

As to any other contexts (just like yours), your guess is as good as mine.
If the compiler manages to deduce it, good. The other version of the same
compiler (even a later version) may not be able to. It doesn't have to.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top