c++ - "typedef void (*task) ()"是做什么的?

标签 c++ typedef

这里部分回答了这个问题 What does "typedef void (*Something)()" mean

但我并不完全清楚答案。

如果我写

typedef void (*task) ();

它是如何展开的?

thread_pool(unsigned int num_threads, task tbd) {
      for(int i = 0; i < num_threads; ++i) {
        the_pool.push_back(thread(tbd));
      }
    }

会不会像这样?

thread_pool(unsigned int num_threads, (*task) () tbd) {
      for(int i = 0; i < num_threads; ++i) {
        the_pool.push_back(thread(tbd));
      }
    }

可能不是,因为这是一个语法错误。我希望你能帮我解决问题。

代码示例来自http://www.thesaguaros.com/openmp-style-constructs-in-c11.html

最佳答案

是这样的:

thread_pool(unsigned int num_threads, void (*tbd) ())

也就是说,类型是函数签名,其中唯一的“词”是“void”。 typedef 名称“task”在此示例中消失了,因为我们不再使用 typedef。

关于c++ - "typedef void (*task) ()"是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14788255/

相关文章:

c++ - 如何替换以下(已弃用)OpenGL 函数?

c++ - 仅为派生类启用模板化基类

c++ - 函数参数中的 Typedef 等价

c++ - typedef class Smth 是什么意思?

c++ - 使用 boost::asio 获取没有标题的 html

c++ - Eigen 为函数的就地版本和非就地版本给出不同的结果

c++ - 为什么这段代码可以在 Coliru 上编译,但不能在 Xcode 上编译?

c++ - 可以将多维 std::array 视为连续的数据 block 吗?

c - 访问另一个 typedef 结构中的 typedef 结构

c - c中同一结构的多个名称