-2

I have come across this toplevel function in a C++ file. So my questions are:

  1. What exactly is the smt2::parser type declaration doing there?
  2. What is this feature called?
  3. How is different from a regular call to p(ctx, is, interactive);?

Here's the code:

bool parse_smt2_commands(cmd_context & ctx, std::istream & is, bool interactive) {
    smt2::parser p(ctx, is, interactive);
    return p();
}

Unfortunately, I haven't been able to find the definition(s) of p() with grep so far. I'll update the post when I find the definitions (tracking down the headers includes manually can take a while).

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
jjpe
  • 664
  • 4
  • 13
  • 1
    look for the function call operator in smt2::parser. See http://stackoverflow.com/questions/2349867/how-can-it-be-useful-to-overload-the-function-call-operator – Kate Gregory Dec 09 '13 at 21:52
  • 1
    That's a C++-style object initialization. –  Dec 09 '13 at 21:53
  • 2
    That's an object instantiation followed by a call to `smt2::parser::operator()`. – David G Dec 09 '13 at 21:53
  • 1
    smt2::parser is a type. p is an instance. The (ctx, is, interactive) are the args to construct p. – Anon Mail Dec 09 '13 at 21:54
  • 1
    Also, OP should have read a beginner tutorial. This is too basic. –  Dec 09 '13 at 21:54
  • possible duplicate of [C++ Instance Initialization Syntax](http://stackoverflow.com/questions/372665/c-instance-initialization-syntax) –  Dec 09 '13 at 21:54
  • Yes gang, `p` is an object that is instantiated in the middle line of the code snippet. What is happening in the last line of the code snippet? – Kate Gregory Dec 09 '13 at 21:54
  • @KateGregory That's not the line OP is asking about... –  Dec 09 '13 at 21:56
  • I agree with @Kate Gregory. It appears to me that smt2::parser 'class' requires an explicit constructor then a default constructor. The object may have some sort of internal state. Bad class, but that's the interface. – KeithSmith Dec 09 '13 at 21:56
  • 2
    @KeithSmith There's no default constructor involved in this code whatsoever. Kate was only right in that `p()` invokes `stm2::parser::operator()`, but that is **not** the line that OP is curious of, nor does it call its default constructor. –  Dec 09 '13 at 21:57
  • OP hasn't asked about the last line because "I haven't been able to find the definition(s) of p() with grep so far" but I am sure it was included for a reason. – Kate Gregory Dec 09 '13 at 21:59
  • Yes. I pulled the trigger too soon. @H2CO3 (are you a chemist?) and @Kate Gregory are correct, `p()` is an overloaded operator, not a constructor. Need more speed, less haste. – KeithSmith Dec 09 '13 at 22:01
  • @KeithSmith No problem. No, I'm not a chemist (I *will* be a bionics engineer, though. My name is related to something else ;) –  Dec 09 '13 at 22:02

2 Answers2

6

It's a little difficult to tell directly. But what I imagine is going on is smt2::parser is the name of a class or struct type. The line

stm2::parser p(ctx,is,interactive);

calls the constructor of that class/struct. The class defined by stm2::parser likely overloaded the () operator. I found a tutorial with more information about operator overloading for function calls.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Behram Mistree
  • 4,088
  • 3
  • 19
  • 21
3

In the code provided in the OP, p is an object of type smt2::parser. It calls a constructor that takes three arguments. p() calls an overloaded operator (probably operator()()) that returns a bool.

Here's an example:

class Test {
public:
Test(int i, char j, bool k) {
}
~Test() { }

bool operator()() const {
return true;
}
};

In this case, we simply call the operator on a temporary to demonstrate what is happening.

int main() {
std::cout << Test(1, 'a', true)(); // 1
return 0;
}