由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ template function type
相关主题
一个关于C++ template和overload的问题C++ template question
谁给详细说一下这句C++ template
一个C++ template的问题有段c++代码看不懂
C++ namespace 弱问template question
C++ Q04: template membera c++ question
C++ template question文一个简单的c++
请问这是什么错误呀问个c++的template的问题
C++ linking 弱问 (one file)template 疑问
相关话题的讨论汇总
话题: mapper话题: hist话题: size话题: typename话题: namespace
进入Programming版参与讨论
1 (共1页)
g****y
发帖数: 436
1
请问下面MAPPER的type应该如何写?先谢过了!
//hist_helper.h
namespace hist_aux {
template<
typename C,
typename T,
typename MAPPER
>
void getHist(const T& vals,
MAPPER pf,
C& res) {

typename T::const_iterator it;
for (it = vals.begin(); it != vals.end(); ++it) {
res.at(pf(*it))++;
}
}
} /* namespace hist_aux */
namespace hist{
class Hist {
public:
// ctr & dtr
// ...

size_t count_mapper(size_t v);
size_t index_mapper(size_t v);
size_t pixel_mapper(size_t v);
void get_histo(const std::vector& vals,
std::vector& result) {
using namespace hist_aux;
getHist // error: missing type info
>
(vals,
std::bind1st(
std::mem_fun(&Hist::count_mapper),
this),
result); // error: no matching function call
}
// others
// ...
};
} /* namespace hist */
t****t
发帖数: 6806
2
你这个写得错误百出的, 看得太费劲了
如果你问的是调用histo的pf应该怎么构造, 应该是
std::bind1st(std::mem_fun(&Hist::count_mapper), this)
因为bind/bind1st做的是bind1st(A, B)(C) --> A(B, C)这样的转换, 但是你如果直接
给A=&T::M, B=this的话, 编译器期待B->*A(C)这样的调用. mem_fun(准确的说是mem_
fun返回的functor)进行的转换是mem_fun(A)(B, ...) -> B->*A(...), which is
exactly what you need.
另外, 你histo的参数和C没有直接的关联, 不能直接推断. 如果你准备在函数调用时显
示给出模板参数的话, 最好把C放第一个.
还有你的mapper返回的都是void, 难道不是返回int或size_t吗?
至于这个设计, 也就这么回事了. C++的funtional大概就是做到这个程度.

【在 g****y 的大作中提到】
: 请问下面MAPPER的type应该如何写?先谢过了!
: //hist_helper.h
: namespace hist_aux {
: template<
: typename C,
: typename T,
: typename MAPPER
: >
: void getHist(const T& vals,
: MAPPER pf,

X****r
发帖数: 3557
3
First, you can't deduce a template parameter from return type.
Second, returning a vector by value is neither efficient nor flexible,
e.g.
what if you want to accumulate histogram?
Third, you don't have to specify vector as input or output.
So what you want is probably something like the following:
template
void histo(const V& vals, const MAPPER& pf, C& result) {
typename V::const_iterator it;
for (it = vals.begin(); it != vals.end(); ++it) {
result.at(pf(*it))++;
}
}

【在 g****y 的大作中提到】
: 请问下面MAPPER的type应该如何写?先谢过了!
: //hist_helper.h
: namespace hist_aux {
: template<
: typename C,
: typename T,
: typename MAPPER
: >
: void getHist(const T& vals,
: MAPPER pf,

g****y
发帖数: 436
4
不好意思,贴代码时临时修改了一些东西。
这个头文件改动过后编译器没有报错,但是class Hist里面的function call我不知道
如何写那个MAPPER的type,所以总觉得是不是可以有简单一点的办法实现这个MAPPER的
多样化。

【在 t****t 的大作中提到】
: 你这个写得错误百出的, 看得太费劲了
: 如果你问的是调用histo的pf应该怎么构造, 应该是
: std::bind1st(std::mem_fun(&Hist::count_mapper), this)
: 因为bind/bind1st做的是bind1st(A, B)(C) --> A(B, C)这样的转换, 但是你如果直接
: 给A=&T::M, B=this的话, 编译器期待B->*A(C)这样的调用. mem_fun(准确的说是mem_
: fun返回的functor)进行的转换是mem_fun(A)(B, ...) -> B->*A(...), which is
: exactly what you need.
: 另外, 你histo的参数和C没有直接的关联, 不能直接推断. 如果你准备在函数调用时显
: 示给出模板参数的话, 最好把C放第一个.
: 还有你的mapper返回的都是void, 难道不是返回int或size_t吗?

g****y
发帖数: 436
5
全部按照你说的改了以后感觉干净多了!我其实不知道如何写写那个MAPPER的type。大
师能再指导一下吗?先谢过了!

【在 X****r 的大作中提到】
: First, you can't deduce a template parameter from return type.
: Second, returning a vector by value is neither efficient nor flexible,
: e.g.
: what if you want to accumulate histogram?
: Third, you don't have to specify vector as input or output.
: So what you want is probably something like the following:
: template
: void histo(const V& vals, const MAPPER& pf, C& result) {
: typename V::const_iterator it;
: for (it = vals.begin(); it != vals.end(); ++it) {

t****t
发帖数: 6806
6
我写了那么多原来白写了...

【在 g****y 的大作中提到】
: 全部按照你说的改了以后感觉干净多了!我其实不知道如何写写那个MAPPER的type。大
: 师能再指导一下吗?先谢过了!

X****r
发帖数: 3557
7
The point is exactly not to explicitly write the type of any
template parameter and let the compiler deduces them.
Just call histo(...)

【在 g****y 的大作中提到】
: 全部按照你说的改了以后感觉干净多了!我其实不知道如何写写那个MAPPER的type。大
: 师能再指导一下吗?先谢过了!

g****y
发帖数: 436
8
...大师的东西营养太多,我只看到前面一小部分就high了,然后去修改代码,然后忘
了后面。。。

【在 t****t 的大作中提到】
: 我写了那么多原来白写了...
g****y
发帖数: 436
9
谢谢指正!编译通过。我自己回去做作业看能不能自己写出那个MAPPER的type。

【在 X****r 的大作中提到】
: The point is exactly not to explicitly write the type of any
: template parameter and let the compiler deduces them.
: Just call histo(...)

t****t
发帖数: 6806
10
连xentar也白写了...

【在 g****y 的大作中提到】
: 谢谢指正!编译通过。我自己回去做作业看能不能自己写出那个MAPPER的type。
1 (共1页)
进入Programming版参与讨论
相关主题
template 疑问C++ Q04: template member
Cannot use my own container as the underlying container of a stack? (c++)C++ template question
请教一下这个template function在gcc下要怎么修改请问这是什么错误呀
c++ template specialization 参数C++ linking 弱问 (one file)
一个关于C++ template和overload的问题C++ template question
谁给详细说一下这句C++ template
一个C++ template的问题有段c++代码看不懂
C++ namespace 弱问template question
相关话题的讨论汇总
话题: mapper话题: hist话题: size话题: typename话题: namespace