是否可以推断或默认较早的模板参数? [英] Is it possible to infer or default an earlier template parameter from a later one?
问题描述
我的目标是c ++ 11(g ++版本4.7)和c ++ 0x(使用icpc 12.something,intel c ++编译器)。我可能会使用clang有一天。
我有以下数据结构:
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){
//通过调用
//使用键来搜索要放在哪里的值$ cmp(item_already_in_mystruct,key) ;
}
};
这个想法是客户可以做
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);
}
或不太蠢的东西。
我现在有这个工作,它不是太疯狂。但我希望我能做得更好。
double_double_compare_fun
和 double_int_compare_fun
已经命名了第二个参数的类型。所以在我的头,我想有一些方法让编译器推断第一个模板参数 insert
。我很想能够说
s.insert< double_double_compare_fun>(4.2,4.2);
s.insert< double_int_compare_fun>(6.2,6);
并且具有 mycmp_t
cmp
或从键
的类型。或者,如果 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);
(给我我也想象使用一个模板如
template< int(* cmp)(const mydata_t& ,const mycmp_t&),typename mycmp_t>
,但它表示签名中的 mycmp_t
。
通常,在惯用的C ++中传递回调的另一种方式:不绑定到特定类型或参数, :
template< class K,class F>
void insert(T const& v,K const& k, F f);
要回答你的实际问题,不,你不能这样做。 / p>
I am targeting c++11 (with g++ version 4.7) and c++0x (with icpc 12.something, the intel c++ compiler). I might use clang some day.
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 do
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 silly.
I currently have this working and it's not too crazy. But I'm hoping I can do better.
double_double_compare_fun
and double_int_compare_fun
already name the type of the second parameter. So in my head, I imagine there's some way to get the compiler to infer the first template argument to insert
. I'd love to be able to say
s.insert<double_double_compare_fun>(4.2, 4.2);
s.insert<double_int_compare_fun>(6.2, 6);
and have mycmp_t
inferred either from the signature of cmp
or from the type of key
. Alternatively, I'd be happy if mycmp_t
could default to the type of the second parameter to cmp
.
I tried variations on this theme and they didn't work, but hopefully it gives some intuition:
template<template<typename mycmp_t>
int (*cmp)(const mydata_t &, const mycmp_t &)
>
void insert(const mydata_t &value, const mycmp_t &key);
(gives me expected 'class' before '(' token, expected identifier before '(' token, expected '>' before '(' token
). I also imagined using a template like template<int (*cmp)(const mydata_t &, const mycmp_t &), typename mycmp_t>
but it says the mycmp_t
present in the signature isn't defined (yet) and things like that.
Normally, you pass callbacks another way in idiomatic C++: Not bound to a specific type or arguments and as a normal parameter:
template<class K, class F>
void insert(T const& v, K const& k, F f);
To answer your actual question though, no, you can't do that.
这篇关于是否可以推断或默认较早的模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!