类类似于一个函数对象? [英] class similar to a function object?

查看:69
本文介绍了类类似于一个函数对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据结构(实际上它是图形),带有一个模板

参数(每个边和顶点的属性P):


struct graph< P> ;;

struct vertex< P> ;;

struct edge< P> ;;


我也有修改此数据结构的算法。算法的基本

大纲与属性类型无关。所以我

实现了算法的泛型版本和

的函数对象我需要的每种类型的属性。通过这种方式,添加一个新属性

只需要定义属性并实现函数

对象。这很好用,但我对

函数对象有疑问,它看起来或多或少是这样的:


模板< typename P>

struct function_object {

//初始化

void initialize(region< P>& r)const;

void initialize(edge< P>& e)const;

//评估

double evaluate(const edge< P>& e)const;

};


正如您所看到的,我使用模板参数声明了它(并且没有添加

实现)。对于每种类型的属性,我添加一个专门化(当然是

实现)。


struct my_property;


模板<>

struct function_object< my_property {

//初始化

void initialize(region< my_property>& r) const;

void initialize(edge< my_property>& e)const;

//评估

double evaluate(const edge< my_property> & e)const;

};


但我可以在不使用function_object的情况下完成此操作。

有针对此设计的专业人士和/或反对者吗?最初,我的想法是

只添加成员函数的专业化,而不是

全班,但这不起作用。

Suppose I have a datastructure (actually it''s a graph) with one template
parameter (the property P for each edge and vertex):

struct graph<P>;
struct vertex<P>;
struct edge<P>;

I also have an algorithm that modifies this datastructure. The basic
outline of the algorithm is independent of the type of property. So I
implemented a generic version of the algorithm and a function object for
each type of property I need. In this way, adding a new property
involves simply defining the property and implementing the function
object. And that works great, however I have a question about the
function object, which looks more or less like this:

template <typename P>
struct function_object {
// Initialization
void initialize(region<P>& r) const;
void initialize(edge<P>& e) const;
// Evaluation
double evaluate(const edge<P>& e) const;
};

As you can see I declared it with a template parameter (and add no
implementation). For each type of property I add a specialization (with
an implementation of course).

struct my_property;

template <>
struct function_object<my_property{
// Initialization
void initialize(region<my_property>& r) const;
void initialize(edge<my_property>& e) const;
// Evaluation
double evaluate(const edge<my_property>& e) const;
};

But I could as well have done this without using "function_object". Are
there pros and/or contras against this design? Initially, my idea was to
add only a specialization of the member functions only, and not the
whole class, but that doesn''t work.

推荐答案

Jef Driesen写道:
Jef Driesen wrote:

假设我有一个数据结构(实际上它是图表)使用一个

模板参数(每个边和顶点的属性P):


struct graph< P> ;;

struct vertex< P>;

struct edge< P> ;;
Suppose I have a datastructure (actually it''s a graph) with one
template parameter (the property P for each edge and vertex):

struct graph<P>;
struct vertex<P>;
struct edge<P>;



你的意思是,可能是


模板< class Pstruct graph;

template< class Pstruct vertex;

模板< class Pstruct edge;

You mean, probably

template<class Pstruct graph;
template<class Pstruct vertex;
template<class Pstruct edge;


我还有一个修改这个数据结构的算法。
I also have an algorithm that modifies this datastructure.



你的意思是,图形结构(和所有相关的)?

You mean, the graph struct (and all related)?


基本

算法的轮廓与属性的类型无关。所以我

为我需要的每种类型的属性实现了算法的泛型版本和函数对象

。通过这种方式,添加一个新属性

只需要定义属性并实现函数

对象。这很好用,但我对

函数对象有疑问,它看起来或多或少是这样的:


模板< typename P>

struct function_object {

//初始化

void initialize(region< P>& r)const;

void initialize(edge< P>& e)const;

//评估

double evaluate(const edge< P>& e)const;

};


正如您所看到的,我使用模板参数声明了它(并且没有添加

实现)。对于每种类型的属性,我添加一个专业化

(当然是实现)。


struct my_property;


模板<>

struct function_object< my_property {

//初始化

void initialize(region< my_property>& r) const;

void initialize(edge< my_property>& e)const;

//评估

double evaluate(const edge< my_property> & e)const;

};


但我可以在不使用function_object的情况下完成此操作。

是否有针对此设计的专业人士和/或反对者?最初,我的想法

只是仅添加成员函数的特化,并且

不是整个类,但这不起作用。
The basic
outline of the algorithm is independent of the type of property. So I
implemented a generic version of the algorithm and a function object
for each type of property I need. In this way, adding a new property
involves simply defining the property and implementing the function
object. And that works great, however I have a question about the
function object, which looks more or less like this:

template <typename P>
struct function_object {
// Initialization
void initialize(region<P>& r) const;
void initialize(edge<P>& e) const;
// Evaluation
double evaluate(const edge<P>& e) const;
};

As you can see I declared it with a template parameter (and add no
implementation). For each type of property I add a specialization
(with an implementation of course).

struct my_property;

template <>
struct function_object<my_property{
// Initialization
void initialize(region<my_property>& r) const;
void initialize(edge<my_property>& e) const;
// Evaluation
double evaluate(const edge<my_property>& e) const;
};

But I could as well have done this without using "function_object".
Are there pros and/or contras against this design? Initially, my idea
was to add only a specialization of the member functions only, and
not the whole class, but that doesn''t work.



什么行不通?


模板<>

void

function_object< my_property> :: initialize(region< my _property>& r)const

{

// blah

}


见常见问题5.8。


V

-

请在通过电子邮件回复时删除大写''A'

我没有回复最热门的回复,请不要问

What doesn''t work?

template<>
void
function_object<my_property>::initialize(region<my _property>& r) const
{
// blah
}

See FAQ 5.8.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

Jef Driesen写道:
Jef Driesen wrote:

假设我有一个数据结构(实际上它是图表),带有一个模板

参数(每个边和顶点的属性P):


struct graph< P> ;;

struct vertex< P>;

结构边缘< P> ;;
Suppose I have a datastructure (actually it''s a graph) with one template
parameter (the property P for each edge and vertex):

struct graph<P>;
struct vertex<P>;
struct edge<P>;



不使用boost :: graph,然后。

Not using boost::graph, then.


我还有一个修改这个数据结构的算法。算法的基本

大纲与属性类型无关。所以我

实现了算法的泛型版本和

的函数对象我需要的每种类型的属性。
I also have an algorithm that modifies this datastructure. The basic
outline of the algorithm is independent of the type of property. So I
implemented a generic version of the algorithm and a function object for
each type of property I need.



函数对象和属性之间存在一对一的关系吗?

或者是否存在可以具有多个函数的属性

对象?


即sort算法接受每种类型的多个函数对象。

There is a one-to-one relation between function object and property?
Or are there properties for which you could have multiple function
objects?

I.e. the sort algorithm accepts multiple function objects per type.


我有一个关于函数对象的问题,如下所示:


模板< typename P>

struct function_object {

//初始化

void initialize(region< P>& r)const ;

void initialize(edge< P>& e)const;

//评估

double evaluate(const edge< P>& ; e)const;

};
I have a question about the function object, which looks like this:

template <typename P>
struct function_object {
// Initialization
void initialize(region<P>& r) const;
void initialize(edge<P>& e) const;
// Evaluation
double evaluate(const edge<P>& e) const;
};



通常,初始化是在构造函数中完成的。并且

评估

通常命名为operator()。您可能希望继承

std :: unary_function

以使其更兼容STL。

Usually initialization is done in the constructor, of course. And
evaluate
is usually named operator(). You might want to inherit from
std::unary_function
to make it more STL-compatible.


对于每种类型的属性,我都添加了一个带有实现的特化。


struct my_property;


template<>

struct function_object< my_property {

//初始化

void initialize(region< my_property>& r)const;

void initialize (edge< my_property>& e)const;

//评估

double evaluate(const edge< my_property>& e)const;

};


但我可以在不使用function_object的情况下完成此操作。

有针对此设计的专业人士和/或反对者吗?
For each type of property I add a specialization with an implementation.

struct my_property;

template <>
struct function_object<my_property{
// Initialization
void initialize(region<my_property>& r) const;
void initialize(edge<my_property>& e) const;
// Evaluation
double evaluate(const edge<my_property>& e) const;
};

But I could as well have done this without using "function_object". Are
there pros and/or contras against this design?



Pro:你可以使用function_object<将默认参数传递给

算法。

cf. std :: less< Tfor std :: sort。

Contra:它与STL或boost :: graph不兼容。

Pro: you can use function_object<Pas a default parameter to the
algorithm.
cf. std::less<Tfor std::sort.
Contra: it''s not compatible with STL or boost::graph.


最初,我的想法是只添加成员函数

的特化,而不是整个类,但这不起作用。
Initially, my idea was to add only a specialization of the member functions
only, and not the whole class, but that doesn''t work.



经常有效的简单解决方案:转发到免费功能模板,

specialize /

超载。


HTH,

Michiel Salters

Simple solution that often works: forward to a free function template,
specialize/
overload that.

HTH,
Michiel Salters


Victor Bazarov写道:
Victor Bazarov wrote:

Jef Driesen写道:
Jef Driesen wrote:

>假设我有一个数据结构(实际上它是一个图形)
模板参数(每个边和顶点的属性P):

结构图< P> ;;
struct vertex< P> ;;
struct edge< P> ;
>Suppose I have a datastructure (actually it''s a graph) with one
template parameter (the property P for each edge and vertex):

struct graph<P>;
struct vertex<P>;
struct edge<P>;



你的意思是,可能是


模板< class Pstruct graph;

template< class Pstruct顶点;

模板< class Pstruct edge;


You mean, probably

template<class Pstruct graph;
template<class Pstruct vertex;
template<class Pstruct edge;



当然。

Of course.


>我也有算法修改此数据结构。
>I also have an algorithm that modifies this datastructure.



你的意思是,图形结构(和所有相关的)?


You mean, the graph struct (and all related)?



这是正确的。

That is correct.


>基本对象。这很有用,但我对
函数对象有疑问,它看起来或多或少是这样的:

模板< typename P>
struct function_object {
//初始化
void initialize(region< P>& r)const;
void initialize(edge< P>& e)const;
//评估
双重评估(const edge< P>& e)const;
};

正如您所看到的,我使用模板参数声明了它(并且没有添加
实现)。对于每种类型的属性,我添加一个专门化
(当然是实现)。

struct my_property;

template<>
struct function_object< my_property {
//初始化
void initialize(region< my_property>& r)const;
void initialize(edge< my_property>& e)const;
/ /评估
双重评估(const edge< my_property>& e)const;
};

但我可以在不使用function_object的情况下完成此任务。<是否有针对此设计的专业人士和/或反对者?最初,我的想法是仅添加成员函数的特化,而不是整个类,但这不起作用。
>The basic
outline of the algorithm is independent of the type of property. So I
implemented a generic version of the algorithm and a function object
for each type of property I need. In this way, adding a new property
involves simply defining the property and implementing the function
object. And that works great, however I have a question about the
function object, which looks more or less like this:

template <typename P>
struct function_object {
// Initialization
void initialize(region<P>& r) const;
void initialize(edge<P>& e) const;
// Evaluation
double evaluate(const edge<P>& e) const;
};

As you can see I declared it with a template parameter (and add no
implementation). For each type of property I add a specialization
(with an implementation of course).

struct my_property;

template <>
struct function_object<my_property{
// Initialization
void initialize(region<my_property>& r) const;
void initialize(edge<my_property>& e) const;
// Evaluation
double evaluate(const edge<my_property>& e) const;
};

But I could as well have done this without using "function_object".
Are there pros and/or contras against this design? Initially, my idea
was to add only a specialization of the member functions only, and
not the whole class, but that doesn''t work.



什么不起作用?


What doesn''t work?



仅提供初始化/评估函数的实现

(没有声明完整的类模板< struct

function_object< my_propertyabove)。因为我认为C ++语言不允许这样做,所以不可能为每种类型的属性添加不同的

成员变量。


模板<>

struct function_object< my_property1 {

int m_data; //假设我需要在这里注明

}


模板<>

struct function_object< my_property2 {

double m_data; //假设我需要双打

}


但那(或编写代码)不是我的主要问题(因为我是

已经有了它的工作。我想知道是否有一个优势超过

做这样的事情:


struct my_function_object {

//初始化

void initialize(region< my_property>& r)const;

void initialize(edge< my_property>& e)const;

//评估

双重评估(const edge< my_property>& e)const;

};


例如,在我的第一篇文章中没有使用模板化的function_object?

Providing only implementations for the initialize/evaluate functions
(without a declaring the full class template <struct
function_object<my_propertyabove). Because I think that is not allowed
by the C++ language and it will make it impossible to add different
member variables for each type of property.

template <>
struct function_object<my_property1{
int m_data; // suppose I need ints here
}

template <>
struct function_object<my_property2{
double m_data; // suppose I need doubles here
}

But that (or writing the code) was not my main problem (because i
already have it working. I was wondering if there is an advantage over
doing something like this:

struct my_function_object {
// Initialization
void initialize(region<my_property>& r) const;
void initialize(edge<my_property>& e) const;
// Evaluation
double evaluate(const edge<my_property>& e) const;
};

e.g., without using the templated "function_object" from my first post?


这篇关于类类似于一个函数对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆