Function template with an operator

The fact that it compiles at all suggests to me that it's doable, I'm just at a loss for how to use it! Any suggestions, or is this method of use a non-starter?

384k 77 77 gold badges 659 659 silver badges 1.1k 1.1k bronze badges asked Jul 12, 2009 at 18:49 34.4k 22 22 gold badges 107 107 silver badges 119 119 bronze badges What does it mean "this doesn't work/neither does this"? What happens? Commented Jul 12, 2009 at 18:52 Compiler barfs on you, that's what happens :) Commented Jul 12, 2009 at 18:59

I swear I've seen this question on SO before. I cannot find it though, so obviously one cannot blame you.

Commented Jul 13, 2009 at 10:45 Ah, found it: stackoverflow.com/questions/942170/… Commented Jul 13, 2009 at 10:47

Thanks. Guess I was just hitting the wrong keywords when I tried searching. (Didn't think to try functor. That should have been obvious in retrospect)

Commented Jul 13, 2009 at 19:36

3 Answers 3

You need to specify T .

int i = c.operator()(); 

Unfortunately, you can't use the function call syntax directly in this case.

Edit: Oh, and you're missing public: at the beginning of the class definition.

answered Jul 12, 2009 at 18:52 32.5k 9 9 gold badges 71 71 silver badges 105 105 bronze badges

Nice. Kudos on the quick and accurate answer! Unfortunately that's probably to verbose for my uses (since my code is intended to be called by others I want to avoid confusion) so I'll just have to find another route. Thanks again!

Commented Jul 12, 2009 at 18:58

Yes, you're probably better off defining a get method instead of operator() . Then you could write c.get() .

Commented Jul 12, 2009 at 19:01

Actually, there is an idiom flying around that's used by many: free get function (tuples use get(some_tuple) , boost.variant uses get(some_variant) ). So yours would look like get(c) , with get being defined in MyClass 'es namespace).

Commented Jul 12, 2009 at 20:07

In this case I needed both a call that returned a value and one that didn't. I implemented Evaluate() for the returning version, and Execute() for the non-returning, and then allowed the user to call the () operator as an alternative to Evaluate. (The names make sense in context, since they are executing functions defined in a script.) So essentially, yes, I implemented a get function :) Thanks for all the suggestions!