A bug in the official libsigc++ 2.0 example?

507 views Asked by At

I was following the official documentation for libsigc++-2.0 and I found this example:

class AlienDetector
{
public:
    AlienDetector();

    void run();

    sigc::signal<void> signal_detected;
};

void warn_people()
{
    cout << "There are aliens in the carpark!" << endl;
}

int main()
{
    AlienDetector mydetector;
    mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );

    mydetector.run();

    return 0;
}

As you can see both the run() function and the constructor for the AlienDetector class are not defined and therefore this code shouldn't compile ( but the doc takes for granted the fact that this code will work ).

Even more strange is the fact that if I define both run() and the constructor for the class, I can't see the effect of the library anyway, apparently the signal doesn't work and when run is called in the main no slot is activated.

I am forgetting about something here ? How this thing should be re-written ?

2

There are 2 answers

4
Anya Shenanigans On BEST ANSWER

The documentation seems to be incomplete.

The most basic version of the code should look like:

AlienDetector::AlienDetector() {}

void AlienDetector::run() {
    sleep(3); // wait for aliens
    signal_detected.emit(); // panic
}

I've posted a fully functional make-based example on github, example1.cpp is the first example, example2.cpp is one using a member function.

0
JaMiT On

To be more precise, that particular page of documentation is correct and presented as intended, but with a key link missing. I'll get to what is missing after reviewing what is there. The code that is listed as one block in the question is presented as two blocks in the documentation:

Here's my class:

<first code block, which
declares AlienDetector>

Here's your code that uses it:

<second code block, which
defines warn_people() and
main()>

This split is an important detail. The learning scenario being presented is one where you have to write code that interfaces with a certain class. In this sort of scenario, the information you have to work with is the declaration of that class, not the implementation of it (the header file, not the source). Hence, the first code block provides the declaration without code for the functions. The presentation appropriately leaves out the details that you should ignore at the moment.

The omission comes a bit further down the page:

To compile this example, use:

g++ example1.cc -o example1 `pkg-config --cflags --libs sigc++-2.0`

Hold on—"example1.cc"? What is that? You might notice that the page did not say how to create example1.cc. While it is somewhat natural to assume that one should copy the code from the page into this file, that is not what was intended. The author had intended that example1.cc be a pre-made file (with the missing function implementations) that would accompany the text. Somehow this file, and the others referenced later in the tutorial, never made it into the published product.

This omission has been known for a while (see some mail archives), but has not attracted the right sort of attention to fix it. (The tutorial was updated a few months after the emails I linked to, but apparently not to add the missing example files.) In the linked emails, the author provided a link to get the missing files, but in the decade+ since then, the link has expired.

Fortunately, Petesh has provided a longer-lived source for some example files in the older answer here. (As for me, I'm just being overly detailed and precise. Too bad I got lost when trying to figure out where this issue could be logged so that it could be addressed, or at least tracked.)