0

There is the following code. The Test function can either accept or not a slot as the parameter. Is there a way to achieve the same but using only the boost::signal2 library?

#include <boost/signals2.hpp>
#include <boost/optional.hpp>
#include <string>
#include <iostream>

typedef boost::signals2::signal<std::string (void)> CSignal;
typedef CSignal::slot_type CSlotType;
typedef boost::optional<CSlotType> COptSlotType;

void Test(const COptSlotType &optSlot = COptSlotType()) {
    std::string str;
    if (optSlot) {
        CSignal sig;
        sig.connect(*optSlot);
        const auto rt = sig();
        if (rt) {
            str = *rt;
        }
    }
    else {
        str = "No Slot";
    }
    std::cout << str << std::endl;
}

std::string TestSignal(void) {
    return "Signal";
}

int main(int argc, char *argv[]) {
    Test();
    Test(COptSlotType(&TestSignal));
    return 0;
}
megabyte1024
  • 8,482
  • 4
  • 30
  • 44
  • `const COptSlotType &optSlot = nullptr` is that actually valid? Defaulting a reference to `nullptr`? – s3rius Jun 12 '13 at 18:00
  • @s3rius, MVC++ 2010 compiles it without problems. – megabyte1024 Jun 12 '13 at 18:02
  • @s3rius, As far as I understand it equals to `const COptSlotType &optSlot = COptSlotType()` – megabyte1024 Jun 12 '13 at 18:06
  • 3
    @megabyte1024 "compiles it without problems" != "actually valid". Even more so when MS compilers are the topic of discussion... – twalberg Jun 12 '13 at 18:33
  • @twalberg, agree. I changed `nullptr` to `COptSlotType()` – megabyte1024 Jun 12 '13 at 18:49
  • @s3rius, I am checked. The `nullptr` is actually valid, because the `boost::optional` has the `optional( none_t none_ )` constructor. The none_t type evaluates to `false`. [Here](http://stackoverflow.com/questions/10934626) are explanations why it is implemented in such way. And `nullptr` is converted to false (Explained [here](http://en.cppreference.com/w/cpp/language/implicit_cast) in the Boolean conversions paragraph). – megabyte1024 Jun 18 '13 at 19:05
  • @twalberg. The nullptr is actually valid. See my comment above. – megabyte1024 Jun 18 '13 at 19:06
  • @megabyte1024 I realize `nullptr` is valid. I'm not entirely so sure about assigning `nullptr`, which is, well, a pointer, to a `const` reference. I guess the idea is to have something like an unbound reference, though... – twalberg Jun 18 '13 at 19:14
  • @twalberg, `nullptr` is implicitly converted to `boost::optional<...>(boost::none)` – megabyte1024 Jun 18 '13 at 19:27
  • I suppose maybe that's true with your current compiler. But it seems to me this might be a future problem, as `nullptr` is a keyword in C++11 (which your current stated compiler obviously does not support)... – twalberg Jun 18 '13 at 19:47
  • @twalberg, it is not a compiler issue. This code works on g++ v.4.7.2 and v.4.8.1 also. It is the `boost::optional` implementation specific. [Here](http://coliru.stacked-crooked.com/view?id=ae645236fc60e386aac81791885fb8c7-92a6b8b905b3338bdfc1eb08c231d068) is a similar code (uses boost::optional) which is available online. – megabyte1024 Jun 18 '13 at 20:20
  • @twalberg, I hope [this online code](http://coliru.stacked-crooked.com/view?id=78fd16db40e9852730a8b0f16f1f9b5e-92a6b8b905b3338bdfc1eb08c231d068) will help you to understand why `nullptr` is not a compiler issue. – megabyte1024 Jun 18 '13 at 20:39

0 Answers0