function-object

Detailed difference between functor's call and function call?

依然范特西╮ 提交于 2019-12-04 23:49:00
The key reason this works is that for_each () doesn’t actually assume its third argument to be a function. It simply assumes that its third argument is something that can be called with an appropriate argument. A suitably defined object serves as well as – and often better than – a function. For example, it is easier to inline the application operator of a class than to inline a function passed as a pointer to function. Consequently, function objects often execute faster than do ordinary functions. An object of a class with an application operator (§11.9) is called a functionlike object, a

Copy constructor is called many times when constructing a thread by function object

旧街凉风 提交于 2019-12-04 13:38:19
问题 I try to pass a function object to a thread. I am confused when I found the copy constructor is called two times in the 'main' thread. Why not simply copy once instead of twice? The second copy is useless. Code Snippet: #include <iostream> #include <thread> using namespace std; struct A { A() { cout << "constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } A(const A &other) { cout << "Copy constructor this=" << this << " thread_id=" << this_thread::get_id() << endl;

Are function pointers function objects in C++?

左心房为你撑大大i 提交于 2019-12-03 13:47:43
The C++ standard defines function objects as: A function object type is an object type that can be the type of the postfix-expression in a function call. ( link ) First I was thinking that function objects were functors, but then I realized that for a function pointer ptr of type P (not a function, but a function pointer), std::is_object_v<P> is true and can be called with the ptr(Args...) syntax. I am right that function pointers are considered as function objects by the standard? And if they are not what part of the definition is not satisfied by function pointers? Yes, they are. The term

In C++ what does it mean for a compiler to “inline” a function object?

随声附和 提交于 2019-12-03 11:36:15
问题 In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them. I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help! 回答1: The last parameter of for_each template is a functor . Functor is something that can be "called" using the () operator (possibly with arguments). By defintion, there are two distinctive kinds of functors:

Copy constructor is called many times when constructing a thread by function object

不羁岁月 提交于 2019-12-03 08:28:27
I try to pass a function object to a thread. I am confused when I found the copy constructor is called two times in the 'main' thread. Why not simply copy once instead of twice? The second copy is useless. Code Snippet: #include <iostream> #include <thread> using namespace std; struct A { A() { cout << "constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } A(const A &other) { cout << "Copy constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } void operator()() { cout << "operator() this=" << this << " thread_id=" << this_thread::get_id() <<

Are functors actually faster than pointers to functions?

倖福魔咒の 提交于 2019-12-03 04:02:37
问题 According to Scott Meyers, one area where C++ shines over C is that function objects are faster than function pointers. He says this is because function objects are inlined, which increases speed. I have two questions about this: How can we verify that function objects are, in fact, inlined? Can we verify this in practice? Does the inlining of function objects depend on the compiler we use, or do all compilers behave like this? 回答1: The C++ and C standards leaves a bunch of freedom to

In C++ what does it mean for a compiler to “inline” a function object?

扶醉桌前 提交于 2019-12-03 02:03:22
In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them. I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help! The last parameter of for_each template is a functor . Functor is something that can be "called" using the () operator (possibly with arguments). By defintion, there are two distinctive kinds of functors: Ordinary non-member functions are functors. Objects of class type with overloaded () operator (so called

Overloading multiple function objects by reference

非 Y 不嫁゛ 提交于 2019-12-03 01:02:44
问题 In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject, returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int

Overloading multiple function objects by reference

浪尽此生 提交于 2019-12-02 16:21:59
In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject , returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int main() { auto o = overload([](char){ cout << "CHAR"; }, [](int) { cout << "INT"; }); o('a'); //

Why doesn't N3421 provide the noexcept qualifier?

余生长醉 提交于 2019-12-01 06:40:47
In N3421 - Making Operator Functors greater<> , the new specialization for the std function objects is: template <> struct plus<void> { template <class T, class U> auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) + std::forward<U>(u)); }; instead of template <> struct plus<void> { template <class T, class U> auto operator()(T&& t, U&& u) const noexcept(noexcept(decltype(std::forward<T>(t) + std::forward<U>(u)) (std::move(std::forward<T>(t) + std::forward<U>(u))))) -> decltype(std::forward<T>(t) + std::forward<U>(u)); }; Is there a reason for that? Does the omission of