Is it possible to infer or set earlier an earlier template parameter from a later one?

I am targeting C ++ 11 (with g ++ version 4.7) and C ++ 0x (with icpc 12.something, intel C ++ compiler). I could one day use clang.

I have the following data structure:

template<typename mydata_t>
struct mystruct {
    template<typename mycmp_t,
             int (*cmp)(const mydata_t &, const mycmp_t &)
             >
    void insert(const mydata_t &value, const mycmp_t &key) {
        // use key to search for where to put value, by calling
        // cmp(item_already_in_mystruct, key);
    }
};

The idea is that the client could

int double_double_compare_fun(const double &d1, const double &d2) { ...; }
int double_int_compare_fun(const double &d, const int &i) { ...; }
int main(void) {
    struct mystruct<double> s;
    s.insert<double, double_double_compare_fun>(4.2, 4.2);
    s.insert<int, double_int_compare_fun>(6.2, 6);
}

or something less stupid.

I have this job right now and it’s not too crazy. But I hope I can do better.

double_double_compare_funand double_int_compare_funalready called the type of the second parameter. Therefore, in my head, I assume that there is a way to get the compiler to output the first argument of the template insert. I would like to say

s.insert<double_double_compare_fun>(4.2, 4.2);
s.insert<double_int_compare_fun>(6.2, 6);

mycmp_t, cmp, key. , , mycmp_t cmp.

, , , , :

template<template<typename mycmp_t>
         int (*cmp)(const mydata_t &, const mycmp_t &)
         >
void insert(const mydata_t &value, const mycmp_t &key);

( expected 'class' before '(' token, expected identifier before '(' token, expected '>' before '(' token). template<int (*cmp)(const mydata_t &, const mycmp_t &), typename mycmp_t>, , mycmp_t, , .

+2
1

++: :

template<class K, class F>
void insert(T const& v, K const& k, F f);

, , .

+1

All Articles