简体   繁体   中英

What do parentheses mean within angle brackets for a Boost signal?

In my basic c++ book, there is no class declaration like below. the strange code for me is ...

boost::signals2::signal<bool (const std::string& message, 
const std::string& caption, unsigned int style),
boost::signals2::last_value<bool> > ThreadSafeMessageBox;

the things in round brackets ( const std:::string ...) are not a typename but instance. How could it be possible? The code above compiles fine.

ps template class ( signal ) code is

template<typename Signature,
  typename Combiner = optional_last_value<typename boost::function_traits<Signature>::result_type>,
  typename Group = int,
  typename GroupCompare = std::less<Group>,
  typename SlotFunction = function<Signature>,
  typename ExtendedSlotFunction = typename detail::extended_signature<function_traits<Signature>::arity, Signature>::function_type,
  typename Mutex = mutex >
class signal: public detail::signalN<function_traits<Signature>::arity,
  Signature, Combiner, Group, GroupCompare, SlotFunction, ExtendedSlotFunction, Mutex>::type
{ /*...*};

Check out the documentation for Boost.Signals2 :

The Boost.Signals2 library is an implementation of a managed signals and slots system. Signals represent callbacks with multiple targets

So we know that a "signal" has something to do with "callbacks". A callback is a function to call at a later time.

So, then check out the "Hello World" example in the documentation:

struct HelloWorld
{
  void operator()() const
  {
    std::cout << "Hello, World!" << std::endl;
  }
};
// ...

  // Signal with no arguments and a void return value
  boost::signals2::signal<void ()> sig;

  // Connect a HelloWorld slot
  HelloWorld hello;
  sig.connect(hello);

  // Call all of the slots
  sig();

First, we create a signal sig , a signal that takes no arguments and has a void return value. Next, we connect the hello function object to the signal using the connect method. Finally, use the signal sig like a function to call the slots, which in turns invokes HelloWorld::operator() to print "Hello, World!".

After reading all this, what can we deduce? We can deduce that the template argument to a signal is a function type . It indicates the kind of function that can be connected to the signal.

So, in your example

boost::signals2::signal<bool (const std::string& message, 
                             const std::string& caption, 
                             unsigned int style), 
                        boost::signals2::last_value<bool> 
                       > ThreadSafeMessageBox;

ThreadSafeMessageBox is a signal that can connect to a function that:

  • returns bool
  • takes a first argument of const std::string&
  • takes a second argument of const std::string&
  • takes a third argument of unsigned int

(The second template argument we can ignore for the purposes of this question, it is not a required template argument nor is it part of the callback function signature, but rather something called a Combiner )

The type expected as template parameter Signature is a function signature, that is the specification of the expected function parameters number, types and return type.

In your case

boost::signals2::signal<bool (const std::string& message, const std::string& caption, unsigned int style), boost::signals2::last_value<bool> > ThreadSafeMessageBox;

the first parameter for the template boost::signals2::signal is the function signature:

bool (const std::string& message, const std::string& caption, unsigned int style)

that is a function with 3 parameters (of type string , string and unsigned int ) and returning a bool .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM