English 中文(简体)
以数值表示的通过率(C++)
原标题:Passing functor object by value vs by reference (C++)

比较通用一体化功能:

template <class F> double integrate(F integrand);

iii

template <class F> double integrate(F& integrand);

template <class F> double integrate(const F& integrand);

每种东西有什么利弊? STL采用第一个方法(超值),这是否意味着它是最普遍的吗?

最佳回答

Function objects usually should be small so I don t think that passing them by value will suffer from performance noticably (compare it to the work the function does in its body). If you pass by value, you can also gain from code analysis, because a by value parameter is local to the function and the optimizer may tell when and when not a load from a data member of the functor can be omitted.

如果胜利者是无国籍者,作为论点,它就意味着根本不承担任何费用,那么ctor子的 takes子就不必有任何特殊价值(至少是海湾合作委员会使用的阿比乌姆)。 在使用参考资料时,你总是必须通过地址。

最后一个(const T&)的缺点是,在C++03中,没有从事原材料,因为在C++03中,如果你试图将const应用到某种功能类别(并且是SFINAE案件),那么方案就缺乏信息。 更近期的执行情况在职能类型上适用时忽视了<代码>const。

第二项(T&)显然有缺陷,无法通过临时校长。

简言之,除非我看到在具体案件中有明显的好处,否则我一般会以价值的方式予以通过。

问题回答

STL采用第一种办法(折合价值)

Sure, the standard libraries pass iterators and functors by value. They are assumed (rightly or wrongly) to be cheap to copy, and this means that if you write an iterator or a functor that is expensive to copy, you might have to find a way to optimize that later.

But that is just for the purposes for which the standard libraries use functors - mostly they re predicates, although there are also things like std::transform. If you re integrating a function, that suggests some kind of mathematics libraries, in which case I suppose you might be much more likely to deal with functions that carry a lot of state. You could for example have a class representing nth order polynomials, with n+1 coefficients as non-static data members.

在这种情况下,最好参考。 在使用诸如<代码>transform等标准算法时,你可以将其归入一个小类别,通过一个点子进行间接交易,以确保复制费用仍然较低。

采用非最基本的提法可能会令用户感到厌恶,因为它阻止用户通过时常。

鉴于此情形,F预计将是一个“可转让物体”(有些如自由功能或由经营者确定的一个类别)

Now, since a free function name cannot be an L-value, the second version is not suitable for that. The third assumes F::operator() to be const (but may not be the case, if it requires to alter the state of F) The first operates on a "own copy", but requires F to be copyable.

其中没有一个是“大学”,但第一个最有可能在最常见的情况下工作。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签