second set of parentheses in function declaration???

I tried finding this myself, but its difficult to describe so I haven't been able to find the answer, although I'm sure its simple... I'm try to learn Intel's threaded building blocks, and as part of the tutorial they have shown the following code:

void operator () (const blocked_range <size_t>& r) { // function here}

all this is within a class declaration. I would have included the whole thing but I'm using android and the pdf viewer doesn't let me select text.. in any case I'm not after what it means, just what that particular syntax is doing. is it declaring operator? I have read up and used templates before, but I've just never seen this so I'm stumped...
Last edited on
It overloads operator ().
I'm not sure what to explain about this.
See http://www.cplusplus.com/doc/tutorial/classes2/
and http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
great. that's all I was after thanks!
one other thing, what does the second "const" in this construction do:

void operator () (const blocked_range <size_t>& r) const { // function here}

more specifically, what exactly is it defining as constant?
Last edited on
Declaring methods as const is the way to tell the compiler which methods are safe to call on const object instances. These methods are compiler enforced to not alter the state of the object.

Assuming that this operator overload is a part of "SomeClass"

1
2
3
4
5
6
7
8
9
class SomeClass
{
   private:
      //private declarations
   protected:
      //protected declarations
   public:
      void operator () (const blocked_range <size_t>& r) const { /* function here*/}
};


then the second "const" is saying that this operator can be called on a const instance of type SomeClass.

i.e. the following will work:
1
2
3
4
5
6
7
8
9
int main()
{
    blocked_range<size_t> blocked_range_instance;

   const SomeClass someClassConstInstance;
   someClassConstInstance(blocked_range_instance); //OK. Will not produce compiler error

   return 0;
}


If the second const were not there, then the above code would produce a compiler error because you'd be trying to invoke a non-const method of a const object instance.
Last edited on
Thanks :)
Topic archived. No new replies allowed.