Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I often write code where I have the following scenario.
I have a (pure) function f and another function g which is templated so that it takes a callable object, say with similar type as f, as an argument.
Now often f is not super trivial but a couple of lines long and since it
is it is stateless I define f as a normal function. But then when passing f I have to write &f and I really dislike the syntax.
Another way would be to write a global lambda or a functor, that would fix my "& issue". But syntactically I prefer normal functions...
So is there a way to define g such that I can pass f as a normal function but avoid the &?
You do not have to explicitly take the address of a function when passing it. As an example, the following code compiles and works:
template <typename F>
void foo(F f) { f(); }
void bar() { }
int main()
{
foo(bar);
}
live example on wandbox.org
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What does this syntax mean?
ForExample<Something>();
Can someone explain with a few examples how and what for it can be used?
This code could be a call of the template function or instantiation of temporary class object.
For example, function is defined as:
template <typename T>
void ForExample(){
// Do something
}
This function can be called as:
ForExample<int>();
In your case type Something can be any type (int, double, string, float ...)
Or we can define template class:
template <typename T>
class ForExample {
// something
};
Temporary object can be created as:
ForExample<int>();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Seeing that QT has a piece of code like this, how can new return an object instead of a pointer?
You made the wrong conclusion. new always returns a pointer. You need to read the documentation of the classes you are using to know how their constructor works. One way to enable foo f = new foo; is the following:
struct foo {
foo(foo*){}
foo(){}
};
int main(){
foo f = new foo;
}
Note that = here is not assignment! The object f is initialized by calling its constructor taking a pointer to a foo.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to do a bit of refactoring and I am curious about how would you approach this problem.
Basically I'm trying to create an initialization function for each class. There are classes that inherit from some others, and i would like to use parent initialization function if possible. How would you address this?
I would like to use these structs with memcpy and maybe using also them with the keywords align and __attribute__((packed)); and they must be usable with extern "C". I would exclude then constructors and destructors.
An example to explain:
struct A
{
int a;
};
void initialize(A& a)
{
a = 0;
}
struct B : A
{
int b;
};
void initialize(B& b)
{
initialize(b); // here I want void initialize(A& a), not recursion
b = 0;
};
Maybe I have to do some kind of cast? Ideally I'm looking a solution that does not create overhead.
Use a static_cast.
In your code, the initialize(b) call will recurse infinitely, because b is better matched as B& than as A& (the argument of the function you want to call), thus the overload resolution picks the same function and recurs.
You specified that you want to initialise the A part of the b object. Why not tell that to the compiler? Tell it that you want to call initialise in it as though it was an A, like so:
initialize(static_cast<A&>(b));
As for your concern that you mentioned in the comment - no copies are being made here. If I used static_cast<A>, however, a temporary object would be created, but that's not the case. I am not casting b to an object of a type A. I am casting it to a reference of a type A, which will result in creation of temporary reference. Since A& matches with A& better than with B&, the first function will be chosen, thus avoiding the recursion.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
as little follow up to this: C++ lambda function without C++0x?
I have created the lambda function as a function object without c0x
the question now is:
how to pass it as a callback/function pointer to another function?
My first try was like this, but it didn't work:
Lambda Obj( C, D);
command ( Obj.operator()(typeA A, typeB B));
I marked the other question to early i guess, so noone looked at the edit.
If you cannot find std::function in std::tr1::function or via std::function or via a compiler upgrade...
Write your own std::function-like type eraser, or use boost, or use 'the fastest possible delegates' (that should google to a complete implememtation), or pass both a this pointer and a method pointer as template-type-deduced arguments to a function, or pass a template-type-deduced copy of the function object to the function.
Note that the last two options require the function's body to be exposed (such as in a header file).
Or convert the function object into a void* style C callback.
I would go with C++11, and failing that boost, and failing that fast delegates, failing that write a type eraser myself, failing that stick body in header and template+pass a copy (unless function is simple, in which case do this first), and failing that pvoid C style.
Using Boost.Bind:
void command(boost::function<void()> func)
{
func();
}
Lambda Obj(C, D);
command(boost::bind<void>(Obj, A, B));
(or maybe you wanted to have):
void command(boost::function<retType(typeA, typeB)> func)
{
retType ret = func(A, B);
}
Lambda Obj(C, D);
command(boost::bind<retType>(Obj, _1, _2)); // placeholders
Using templates (the STL's way):
template <typename F>
void command(F func)
{
func(A, B);
}
Lambda Obj(C, D);
command(Obj);
Live demo link.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
New to c++ - kind of been cramming the last few days. Things are going pretty well! I do have one question though.
If I make a template:
template <class T>
T testFunc(T t1, T t2)
{
// code code code code
}
My question, is the template <> line specific to that ONE function underneath? I couldn't continue to use the T placeholder in further functions could I?
like:
template <class T>
T testFunc(T t1, T t2)
{
// code code code code
}
T testFunc2(T t1, T t2)
{
// This one does other things....;
}
Well when you declare
template <class T>
T testFunc(T t1, T t2)
This means that you are declaring a generic function "testFunc" which takes any class and works with it. To be honest, I don't really know how it can be useful. If you are defining your function in the global namespace then you can use as many template 's above your functions as you like. They are in now way connected.
You can make a generic class that takes another class as a template. That way you don't have to write "template " above every single method in your class. But your methods CAN take the type T as an argument.