M
Marcel Müller
I want to cast a template argument. Unfortunately gcc creates an error
as if the cast has never been there. Is this not allowed generally?
-----
#include <string.h>
// Sorted vector of T with the key type K using the comparer C.
template <class T, class K, int (*C)(const T* e, const K* k)>
class sorted_vector
{public:
// Find an element by it's key.
// The function will return NULL if no such element is in the container.
T* find(const K* key) const;
// Ensure an element with a particular key.
// This will either return a reference to a pointer to an existing object
// which equals to key or a reference to a NULL pointer which is
// created at the location in the container where a new object with key
// should be inserted.
T*& get(const K* key);
// Erase the element which equals key and return the removed pointer.
// If no such element exists the function returns NULL.
T* erase(const K* key);
};
// data structure, POD
struct data
{ char Key[10];
char Value[10];
};
// Works
int (*const comparer)(const data*, const char*)
= (int (*)(const data*, const char*))&strcmp;
// does not work, see below
typedef sorted_vector<data, char,
(int (*)(const data*, const char*))&strcmp> stringmap;
-----
test6.cpp:34: error: could not convert template argument `strcmp' to `int
(*)(const data*, const char*)'
Of cource, strcmp does not compare against objects of type data, but
since data is a POD type the equivalence of const data* and const char*
should not raise serious problems, and a helper function should be
superfluous.
Marcel
as if the cast has never been there. Is this not allowed generally?
-----
#include <string.h>
// Sorted vector of T with the key type K using the comparer C.
template <class T, class K, int (*C)(const T* e, const K* k)>
class sorted_vector
{public:
// Find an element by it's key.
// The function will return NULL if no such element is in the container.
T* find(const K* key) const;
// Ensure an element with a particular key.
// This will either return a reference to a pointer to an existing object
// which equals to key or a reference to a NULL pointer which is
// created at the location in the container where a new object with key
// should be inserted.
T*& get(const K* key);
// Erase the element which equals key and return the removed pointer.
// If no such element exists the function returns NULL.
T* erase(const K* key);
};
// data structure, POD
struct data
{ char Key[10];
char Value[10];
};
// Works
int (*const comparer)(const data*, const char*)
= (int (*)(const data*, const char*))&strcmp;
// does not work, see below
typedef sorted_vector<data, char,
(int (*)(const data*, const char*))&strcmp> stringmap;
-----
test6.cpp:34: error: could not convert template argument `strcmp' to `int
(*)(const data*, const char*)'
Of cource, strcmp does not compare against objects of type data, but
since data is a POD type the equivalence of const data* and const char*
should not raise serious problems, and a helper function should be
superfluous.
Marcel