gpt4 book ai didi

c++ - Boost 信号的尖括号内的括号是什么意思?

转载 作者:行者123 更新时间:2023-11-30 02:22:12 36 4
gpt4 key购买 nike

在我的基础 C++ 书中,没有像下面这样的类声明。对我来说奇怪的代码是......

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

圆括号中的东西 (const std::::string...) 不是类型名而是实例。怎么可能?上面的代码可以正常编译。

附注模板类(signal)代码为

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
{ /*...*};

最佳答案

查看 Boost.Signals2 的文档:

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

所以我们知道“信号”与“回调”有关。 callback是稍后调用的函数。

所以,然后查看 "Hello World"文档中的示例:

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!".

看完这一切,我们能推断出什么?我们可以推断出信号的模板参数是一个函数类型。它表示可以连接到信号的函数类型。

所以,在你的例子中

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

ThreadSafeMessageBox 是一个可以连接到函数的信号:

  • 返回 bool 值
  • 采用 const std::string& 的第一个参数
  • 接受 const std::string& 的第二个参数
  • 接受 unsigned int 的第三个参数

(出于这个问题的目的,我们可以忽略第二个模板参数,它不是必需的模板参数,也不是回调函数签名的一部分,而是称为 Combiner 的东西)

关于c++ - Boost 信号的尖括号内的括号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47593790/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com