With newer C++ features, you often give a function as a parameter, for example:
// File A.cpp
void do_something(Foo* foo) { ... }
void A::process_foo(){
for_each( foo_list.begin(), foo_list.end(), do_something );
}
But where should I actually put the function do_something(...) when I work with classes? I can not make it a private member, since I would loose this when passing the parameter to for_each.
So I tend to just define a plain function do_something(...) in my implementation file A.cpp, like given in the code above. Since this is visible by the implementation of A only, I do not risk namespace pollution. Since a similiar function in other classes would also only be visible in their implementation, I also do not risk to have a name collision with a similiar function of another class.
Is this the right way?
Another idea would be to use a Lambda. I'm not very familiar with Lambdas, so I don't know whether I should use them as much as possible or only if absolutely necessary...
The third argument of std::for_each needs to be function or function object with one argument such as it may be called with an element of the range defined by first two arguments of for_each. Then you have following options (assuming that foo_list stores Foo*):
Use regular function
void do_someting(Foo*){...}
for_each(..., do_something);
You can put the function wherever it is suitable. If this is for local use, the anonymous namespace is the best option. But it may be e.g. defined in a separate compilation unit.
Use static method
static void do_something(Foo*){...}
for_each(..., &Foo::do_something);
Note that it does not need necessarily to be static method of Foo.
Use lambda
for_each(...,[](Foo* f){...});
Use a method of Foo class (even private) and std::bind
void method(){...}
for_each(..., std::bind(&Foo::method, _1));
There are other options but those are the most common.
C++11 solution
If you can use C++11, prefer range-based for instead of std::for_each and just write code in-place. Like this:
for (const auto& value : foo_list)
{
// do something with the value
}
It is less verbose and more convenient. It iterates through all of the elements one by one, just like std::for_each algorithm. And you can explicitly specify that you don't want to modify elements by putting const auto&, or simply auto (without reference).
Partial-C++11
If your compiler has no support of range-based fors, but has support of lambdas (like Visual Studio 2010), simply put function into lambda:
for_each( foo_list.begin(), foo_list.end(),
[] (const FooList::value_type& value) { /* do something with the value */; });
C++98
If you can use none of the above C++11 features, most of STL algorithms look pathetic. Whichever you place do_something function to, it will be decoupled from the calling code, which is very hard to read. Prefer simple iterator-based for in this case:
for (FooList::iterator pValue = foo_list.begin(); pValue != foo_list.end(); ++pValue)
{
// do something with the pValue
}
PS I prefer the latter form even for "Partial-C++11" case, when you cannot use range-based fors, but can replace FooList::iterator with simple auto. It is very helpful when you would have to write something more complicated, like std::list<std::string>::const_iterator. I think the following is better than std::for_each with lambda:
for (auto pValue = foo_list.begin(); pValue != foo_list.end(); ++pValue)
{
// do something with the pValue
}
Related
I have defined an array as:
std::array<std::pair<std::shared_ptr<OpCode>, std::shared_ptr<Argument>>, 256> opcodes;
where Opcode and Argument are base classes for functors for interpreting data in Metafont GF files. A typical implementation looks like:
class skip: public OpCode {
public:
using OpCode::OpCode;
void operator()(std::int_fast32_t argument) override {
character_context->next_line(argument);
character_context->make_white();
}
};
I'd like to be able to do something like:
opcodes[opcode].first(opcodes[opcode].second(opcode));
to interpret each opcode in the GF file. (As an aside, typing that code above makes me think that I'm likely to want to use a custom class in place of pair so that I don't have to have duplicated references to opcodes[opcode] but that’s for a further revision.)
What I've noticed in CLion is that it insists that to be able to call my operator(), it expects me to write something like this instead:
int_fast32_t arg = opcodes[opcode].second->operator()(opcode);
So two questions:
Is there a different/better way to manage my data structure so I don't have to explicitly call operator() like the above?
Is there a more idiomatic structure I should be using to manage my Functors (assuming that they're even the right tool for the job) to enable stepping through the GF byte code?
Is there a different/better way to manage my data structure so I don't have to explicitly call operator() like the above?
Well, if you don't store pointers, you don't need to dereference them. For example, if your functors are flyweights, you don't need to store owning pointers everywhere, and can just use
array<pair<std::reference_wrapper<OpCode>, std::reference_wrapper<Argument>>, 256> opcodes;
Is there a more idiomatic structure I should be using to manage my Functors
if you're always chaining them anyway, you can do that explicitly:
array<std::function<void(int_fast32_t)>, 256> opcodes;
void bind_opcode(int_fast32_t op, OpCode* foo, Argument* bar)
{
opcodes[op] = [=](int_fast32_t x) { (*foo)((*bar)(x)); }
}
(obviously that can use raw pointers, shared pointers, references if you're certain the object lifetimes are guaranteed elsewhere ... or if the OpCode and Argument objects are not that big, you could template the function and just copy the concrete functors by value).
}
I'd like to reduce amount of syntax required to refer to a function and was wondering if there was a way to do something like:
(NOT COMPILABLE)
using pushToLastUsed = mSomeLongStackFIFOObject.push_back;
// or
auto pushToLastUsed = mSomeLongStackFIFOObject.push_back;
then I could to something like:
pushToLastUsed(10);
instead of:
mSomeLongStackFIFOObject.push_back(10);
Of course I could make a macro like:
#define pushToLastUsed mSomeLongStackFIFOObject.push_back
// some code using it here
#undef pushToLastUsed
but I'd prefer not to use macros.
One solution might be to use a lambda expression to capture the function call into a callable object :
#include <vector>
void foo(std::vector<int> & bar)
{
auto pushToLastUsed = [&bar](int index) {
bar.push_back(index);
};
pushToLastUsed(10);
}
Though in my opinion there is very little to gain from doing this, even if you replace bar with a very long identifier.
My first idea was something along the line of the other answer. On a second read of your question I understand that it is mainly the long name of the object that you want to avoid to repeat. Hiding a call to a standard function should be done with care as its main effect is to obfuscate your code. Everybody knows what push_back does, but even you will likely forget what exactly pushToLastUse does. A different option is to alias only the mSomeLongStackFIFOObject with a shorter name as in
auto& short_name = mSomeLongStackFIFIObject;
short_name.push_back(10);
When you're writing mSomeLongStackFIFOObject.push_back(10); you're actually calling SomeLongStackFIFOClass::push_back(&mSomeLongStackFIFOObject, 10);
One option is to do:
auto& m= mSomeLongStackFIFOObject;
And then:
m.push_back(10);
It will shorten it and still let you use any variable you like.
If the variable is global, you can always do:
static inline void pushBack(int n) { mSomeLongStackFIFOObject.push_back(n); }
If you're trying to shorten the access, I can guess that you're using the variable more than once; then it could make sense to try to put all the accesses in a function that belongs to the class.
You can achieve the desired behaviour by binding the object mSomeLongStackFIFOObject to the member function push_back and using a placeholder for its argument. This requires at least a C++11 compiler.
Consider the following example:
#include <functional>
#include <iostream>
struct A {
void push_back(const int& n) { std::cout << "push_back(" << n << ")\n"; }
};
int main() {
A mSomeLongStackFIFOObject;
std::function<void(const int&)> pushToLastUsed = std::bind(
&A::push_back,
&mSomeLongStackFIFOObject,
std::placeholders::_1
);
pushToLastUsed(10); // push_back(10)
}
Some notes about this:
As Mirko already mentioned correctly, calling a non-static member function is basically the same as calling a static member function with this as implicit first parameter. The binding of an instance of struct A as first parameter makes use of this fact.
Type inference using auto does work for member functions without any parameters but not in the case above.
If the non-static member function is overloaded (e.g. std::vector<T>::push_back) you have to explicitly state the template parameters for the function template std::bind. See Using std::tr1::bind with std::vector::push_back
or Are there boost::bind issues with VS2010? for further information.
Currently, I have a struct which I've used to create a lot of parameters, then I have to run various functions on all of them.
Function1(Mom.SayHi);
Function1(Mom.BeNice);
Function1(Mom.MindManners);
Function3(Mom.SayHi);
Function3(Mom.BeNice);
Function3(Mom.MindManners);
and so on, and the issue is that I have a very long list of parameters that will probably expand. Is there a way to run a specified function on everything in the struct, like
xAllTheY(FunctionWut,AllParams){
FunctionWut(AllParams);
}
It seems that I can't use a function as a parameter. It may be because I'm using void functions. Am I missing an asterisk(*) or something?
I'm using MS Visual C++ 2010 Express, and I don't care about portability.
C++ does not have any feature to run a function on every field of the structure. You need to write a call for each field explicitly.
The reason for that is that C++ is sort of "low level language" if comparing it with recent languages. It tends to translate into instructions what is written in the source code.
I believe there are several ways to solve the task.
If you know that all functions have the same signature, you can use function pointer (or member function pointer) and array of potential arguments. Then you iterate over array of potential arguments and pass them to the function pointer. For instance:
void exec(void (*fun)(const std::string&), const std::vector<std::string>& args)
{
for (auto& v: args) { fun(v); }
}
You can achieve the same result (indeed, even better result due to function pointer optimization) by using template function (or functor), which accepts function and arguments list. This way you can adopt arbitrary functions to the same piece of code; moreover, you can adopt functions that accept more than one argument by using std::bind. For instance:
template <typename Fun, typename ArgsContainer>
void exec(Fun fun, const ArgsContainer& args)
{
for (auto& v: args) { fun(v); }
}
Actually, this is already done by for_each algorithm:
for_each(args.begin(), args.end(), &func);
Last but not least, you can use macros. Sometimes using macro+include to iterate over something is acceptable technique. For instance:
void exec(void (*fun)(const std::string&))
{
#define RUN(arg) fun((arg))
#include "run_cases.h"
#undef RUN
}
where run_cases.h looks like:
RUN("test1");
RUN(2.0f);
RUN(anything_that_is_acceptable_as_any_function_overload);
And no, you cannot pass just the name of the struct assuming compiler will substitute it with it's members. You have to explicitly type them in; actually, you don't want every struct member to be used, implicit ones are a good candidate for exclusion.
I read the tutorials about the binary and unary functions. I understood the structure of them, but I couldn't imagine in which case I need these functions. Can you give an example for usage of them.
http://www.cplusplus.com/reference/std/functional/unary_function/
http://www.cplusplus.com/reference/std/functional/binary_function/
These aren't functions, these are classes (structs, actually, but doesn't matter). When you define your own binary functions to use with STL algorithms, you derive them from these classes in order to automatically get all the typedefs.
E.g.
struct SomeFancyUnaryFunction: public std::unary_function<Arg_t, Result_t>
{
Result_t operator ()(Arg_t const &)
{
...
}
};
now you don't need to manually provide the typedefs for argument_type, result_type etc. These structs, just like the iterator struct are there just for our convenience, in order to reuse the typedefs needed for algorithms.
Update for C++11:
As of C++11, the new std::bind does not really need any typedefs, so there are, in a way, obsolete.
Basically, they provide all the typedefs necessary to allow composition of higher-order functions from unary and binary function objects using function adaptors. For example, this allows using a binary functor where a unary is needed, binding one of the arguments to a literal value:
std::find_if( begin, end, std::bind1st(greater<int>(),42) );
std::bind1st relies on the functor passed to it to provide those types.
AFAIK the new std::bind doesn't need them, so it seems in new code you can use std::bindand do away with them.
There's an explanation on the sgi STL documentation of Function Objects. In summary, unary_function and binary_function are used to make functors adaptable. This allows them to be used with function object adaptors such as unary_negate.
What are they?
std::unary_function and std::binary_function are base structs for creation adaptable function objects. The word adaptable means that they provide necessary typedefs for being used in conjunction with standard function adaptors like std::not1, std::not2, std::bind1st, std::bind2nd.
When I need to use them?
You may use them every time you need to use your custom function object together with standard function adaptor.
Do you have an example?
Lets consider some examples (I know, they are artificial. From the other side I hope, that they are rather descriptive).
Example 1.
Suppose you want to print all strings in a vector with their lengths not less than a particular threshold and print them to std::cout.
One might use the next function object:
class LengthThreshold
{
public:
LengthThreshold(std::size_t threshold) : threshold(threshold) {}
bool operator()(const std::string& instance) const
{
return (instance.size() < threshold);
}
private:
const std::size_t threshold;
};
Now the task is pretty simple and can be performed by std::remove_copy_if algorithm:
// std::size_t threshold is defined somewhere
std::remove_copy_if(some_strings.begin(), some_strings.end(),
std::ostream_iterator<std::string>(std::cout, "\n"),
LengthThreshold(threshold)
);
What if you want to use the same function object to print all the strings with their lengths strictly less than the threshold?
The obvious solution we can come up with is the usage of std::not1 function adaptor:
// std::size_t threshold is defined somewhere
std::remove_copy_if(some_strings.begin(), some_strings.end(),
std::ostream_iterator<std::string>(std::cout, "\n"),
std::not1(LengthThreshold(threshold))
);
In fact, the code above won't compile because our LengthThreshold is not adaptable and has no typedefs which are necessary for std::not1.
To make it adaptable we need to inherit from std::unary_function:
class LengthThreshold : public std::unary_function<std::string, bool>
{
// Function object's body remains the same
}
Now our first example works like a charm.
Example 2.
Lets change our previous example. Suppose we don't want to store a threshold inside the function object. In such case we may change the function object from unary predicate to binary predicate:
class LengthThreshold : public std::binary_function<std::string, std::size_t, bool>
{
public:
bool operator()(const std::string& lhs, std::size_t threshold) const
{
return lhs.size() < threshold;
}
};
And make use of std::bind2nd function adaptor:
// std::size_t threshold is defined somewhere
std::remove_copy_if(some_strings.begin(), some_strings.end(),
std::ostream_iterator<std::string>(std::cout, "\n"),
std::bind2nd(LengthThreshold(), threshold)
);
What about C++11 and higher?
All the examples above intentionally use only C++ 03.
The reason is that std::unary_function and std::binary_function are deprecated since C++ 11 and completely removed from C++ 17.
It happened with the advent of more generalized and flexible functions like std::bind which make inheriting from std::unary_function and std::binary_function superfluous.
In the Boost Signals library, they are overloading the () operator.
Is this a convention in C++? For callbacks, etc.?
I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me.
Any insight as to the reason for this overload?
One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it can keep data reflecting its state between calls.
Here is a simple functor example :
struct Accumulator
{
int counter = 0;
int operator()(int i) { return counter += i; }
}
...
Accumulator acc;
cout << acc(10) << endl; //prints "10"
cout << acc(20) << endl; //prints "30"
Functors are heavily used with generic programming. Many STL algorithms are written in a very general way, so that you can plug-in your own function/functor into the algorithm. For example, the algorithm std::for_each allows you to apply an operation on each element of a range. It could be implemented something like that :
template <typename InputIterator, typename Functor>
void for_each(InputIterator first, InputIterator last, Functor f)
{
while (first != last) f(*first++);
}
You see that this algorithm is very generic since it is parametrized by a function. By using the operator(), this function lets you use either a functor or a function pointer. Here's an example showing both possibilities :
void print(int i) { std::cout << i << std::endl; }
...
std::vector<int> vec;
// Fill vec
// Using a functor
Accumulator acc;
std::for_each(vec.begin(), vec.end(), acc);
// acc.counter contains the sum of all elements of the vector
// Using a function pointer
std::for_each(vec.begin(), vec.end(), print); // prints all elements
Concerning your question about operator() overloading, well yes it is possible. You can perfectly write a functor that has several parentheses operator, as long as you respect the basic rules of method overloading (e.g. overloading only on the return type is not possible).
It allows a class to act like a function. I have used it in a logging class where the call should be a function but i wanted the extra benefit of the class.
so something like this:
logger.log("Log this message");
turns into this:
logger("Log this message");
Many have answered that it makes a functor, without telling one big reason why a functor is better than a plain old function.
The answer is that a functor can have state. Consider a summing function - it needs to keep a running total.
class Sum
{
public:
Sum() : m_total(0)
{
}
void operator()(int value)
{
m_total += value;
}
int m_total;
};
You may also look over the C++ faq's Matrix example. There are good uses for doing it but it of course depends on what you are trying to accomplish.
The use of operator() to form functors in C++ is related to functional programming paradigms that usually make use of a similar concept: closures.
A functor is not a function, so you cannot overload it.
Your co-worker is correct though that the overloading of operator() is used to create "functors" - objects that can be called like functions. In combination with templates expecting "function-like" arguments this can be quite powerful because the distinction between an object and a function becomes blurred.
As other posters have said: functors have an advantage over plain functions in that they can have state. This state can be used over a single iteration (for example to calculate the sum of all elements in a container) or over multiple iterations (for example to find all elements in multiple containers satisfying particular criteria).
Start using std::for_each, std::find_if, etc. more often in your code and you'll see why it's handy to have the ability to overload the () operator. It also allows functors and tasks to have a clear calling method that won't conflict with the names of other methods in the derived classes.
Functors are basically like function pointers. They are generally intended to be copyable (like function pointers) and invoked in the same way as function pointers. The main benefit is that when you have an algorithm that works with a templated functor, the function call to operator() can be inlined. However, function pointers are still valid functors.
One strength I can see, however this can be discussed, is that the signature of operator() looks and behaves the same across different types. If we had a class Reporter which had a member method report(..), and then another class Writer, which had a member method write(..), we would have to write adapters if we would like to use both classes as perhaps a template component of some other system. All it would care about is to pass on strings or what have you. Without the use of operator() overloading or writing special type adapters, you couldn't do stuff like
T t;
t.write("Hello world");
because T has a requirement that there is a member function called write which accepts anything implicitly castable to const char* (or rather const char[]). The Reporter class in this example doesn't have that, so having T (a template parameter) being Reporter would fail to compile.
However, as far I can see this would work with different types
T t;
t("Hello world");
though, it still explicitly requires that the type T has such an operator defined, so we still have a requirement on T. Personally, I don't think it's too wierd with functors as they are commonly used but I would rather see other mechanisms for this behavior. In languages like C# you could just pass in a delegate. I am not too familiar with member function pointers in C++ but I could imagine you could achieve the same behaviour there aswell.
Other than syntatic sugar behaviour I don't really see the strengths of operator overloading to perform such tasks.
I am sure there are more knowingly people who have better reasons than I have but I thought I'd lay out my opinion for the rest of you to share.
Another co-worker pointed out that it could be a way to disguise functor objects as functions. For example, this:
my_functor();
Is really:
my_functor.operator()();
So does that mean this:
my_functor(int n, float f){ ... };
Can be used to overload this as well?
my_functor.operator()(int n, float f){ ... };
Other posts have done a good job describing how operator() works and why it can be useful.
I've recently been using some code that makes very extensive use of operator(). A disadvantage of overloading this operator is that some IDEs become less effective tools as a result. In Visual Studio, you can usually right-click on a method call to go to the method definition and/or declaration. Unfortunately, VS isn't smart enough to index operator() calls. Especially in complex code with overridden operator() definitions all over the place, it can be very difficult to figure out what piece of code is executing where. In several cases, I found I had to run the code and trace through it to find what was actually running.
Overloading operator() can make the class object calling convention easier. Functor is one of the applications of operator() overloading.
It is easy to get confused between Functor and user-defined conversion function.
Below 2 examples show the difference between
1. Functor
2. User-defined conversion function
1. Functor:
struct A {
int t = 0;
int operator()(int i) { return t += i; } // must have return type or void
};
int main() {
A a;
cout << a(3); // 3
cout << a(4); // 7 (Not 4 bcos it maintaines state!!!)
}
2. User-defined conversion function:
struct A {
int t = 3;
operator int() { return t; } // user-defined conversion function
// Return type is NOT needed (incl. void)
};
int main() {
cout << A(); // 3 - converts the object{i:3} into integer 3
A a;
cout << a; // 3 - converts the object{i:3} into integer 3
}