-
September 5th, 2011, 08:00 PM
#1
PLease explain this syntax
could some please explain this syntax for C++ functor
Code:
struct add_x {
add_x(int x) : x(x) {} // please expain this
int operator()(int y) { return x + y; } // and this
I was interested is the colon : after add_x(int x)
-
September 5th, 2011, 08:43 PM
#2
Re: PLease explain this syntax
That's an initializer list. You may find one in any constructor.
-
September 5th, 2011, 09:09 PM
#3
Re: PLease explain this syntax
The second one is the operator for casting to int, both explicitly and implicitly with static_cast<int>(object)
Last edited by ninja9578; September 5th, 2011 at 09:11 PM.
-
September 5th, 2011, 09:42 PM
#4
Re: PLease explain this syntax
I know for experienced developers it it might be mundane stuff , please elaborate I am try to port an application to CUDA thrust C++ API , the real easy way is to pass a functor , similar to
std:: transform with vector array CUDA in parallel will perform all calculations in paralles , the thrust::transform works similar to std version except it is a whole lot faster ,
For some reason I have difficulty warping my head around C++ functor construct ( may be a mental block )
-
September 5th, 2011, 10:07 PM
#5
Re: PLease explain this syntax
Originally Posted by ninja9578
The second one is the operator for casting to int, both explicitly and implicitly with static_cast<int>(object)
No, the second one overloads the function call operator such that this function can be invoked using syntax that makes it appear like an add_x object is a function that takes an int argument and returns an int, i.e., a function object (functor).
-
September 6th, 2011, 01:41 AM
#6
Re: PLease explain this syntax
Originally Posted by ninja9578
The second one is the operator for casting to int, both explicitly and implicitly with static_cast<int>(object)
Correct me if I'm wrong, but I think the second one is an overload of the function call operator that returns the sum of the argument and the data member as an int. So, you could do something like this:
Code:
add_x obj(50);
const int y = obj(10);
The value of y would be 60.
-
September 6th, 2011, 07:01 AM
#7
Re: PLease explain this syntax
Originally Posted by aamir121a
could some please explain this syntax for C++ functor
You could do this instead,
Code:
struct add_x {
add_x(int a) {x = a;} // variable assignment of constructor parameter not using an intialization list
int fun(int y) {return x + y}; // definition of your own functor function not following the () convention
For constructors it's considered best practice to use an initialization list. It can also be a little faster.
You can define any functor function you like but since most functor enabled code follows the overloaded () operator convention it's better to stick with it.
Last edited by nuzzle; September 6th, 2011 at 07:10 AM.
-
September 6th, 2011, 08:33 PM
#8
Re: PLease explain this syntax
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|