Compiler Error C2664 when passing pointer to function

Started by
14 comments, last by Tutukun 14 years, 10 months ago
Quote:Original post by Red Ant
By the way, why does C++ even care if the pointer arguments have the same const-qualifiers in both function signatures? I mean they're just local variables ... what's it to C++ if the function changes the value of a local variable. I can understand that equal constness matters with regard to the pointee, but why the pointers themselves? Hmm.

The existence of const function arguments is a great demonstration of the silliness inherent in the C++ const model. Nevertheless, it really does make sense to disallow coercion of this sort, because constness can certainly play into calling conventions. For instance, a particular hypothetical compiler on a hypothetical architecture might decide to pass const arguments through registers that were slower to write than others. (I know, that's a bit contrived.) A function pointer would then have to keep around not only the address to jump to, but information on which arguments need to be passed in different registers. It's the same reason why function pointers don't support argument-type contravariance or return-type covariance.
Advertisement
Quote:Original post by Nitage
Try:
static bool TowerInfoInterface::_CompareIteratorValue(const char const* c1 , const char* const c2)
{
return (strcmp(c1, c2) == 0)? true:false;
}


(Note that strcmp takes two char*s, not two chars so you'll also need to correct your function's body).


Quote:
cannot convert parameter 2 from 'bool (__cdecl *)(const char *const ,const char *const )' to 'bool (__cdecl *)(const T &,const T &)'
1> with
1> [
1> T=char *
1> ]
1> None of the functions with this name in scope match the target type


no luck :(
i might just add an extra function find(T, (bool*)(T,T))
but i really want to know the solution for this
Well, I'll take one final shot at this.

static bool TowerInfoInterface::_CompareIteratorValue(const char const*& c1 , const char*& const c2){    return (strcmp(c1, c2) == 0)? true:false;}
Why not make the find function take a "predicate" as the second template argument?

template <class T, class Predicate>iterator find(const T&, Predicate compare)


This, I believe, is how STL does it. You don't have to worry about unsuitable predicates, since the usage of the predicate would fail to compile, and it is more generic as you can also use function objects.

return (strcmp(c1, c2) == 0)? true:false;


Hm, is the ternary operator really necessary here? :)
You have to do a typedef on char*. This way the compiler knows that you want to have passed a const reference to a char* (whose data can be modified), as opposed to something else.


template <class T>
int find(const T&, bool (*)(const T&, const T&)) {

return 0;
}

typedef char* S;

bool comp(const S &c1, const S &c2) {

return strcmp(c1, c2)==0?true:false;
}

int main() {


char* s;
find (s, comp);

return 0;
}
Quote:Original post by gpu_fem
You have to do a typedef on char*. This way the compiler knows that you want to have passed a const reference to a char* (whose data can be modified), as opposed to something else.




lol i just wake up, try ur suggestion n it works

so simple, why didnt I think of that
thx u :D

This topic is closed to new replies.

Advertisement