使用C 模板函数编译时间递归
Compile time recursive using C++ template functions
以下代码是编译时间递归吗?我想知道如何确认这一点,即任何调试器,探索器等以了解模板程序。
#include <iostream>
#include <vector>
#include <thread>
std::vector<std::thread> vecc;
void thread_fn(){
std::cout<<"Thread function"<<"n";
}
template <int n>
void create_thread(){
create_thread<n-1>();
vecc.push_back(std::thread(thread_fn));
}
template<>
void create_thread<0>(){
vecc.push_back(std::thread(thread_fn));
}
int main()
{
create_thread<10>();
for(auto &a: vecc){
a.join();
}
}
对于GCC,您可以使用-fdump-tree-original
选项:
g++ -fdump-tree-original -Wall -pthread 111.cpp
现在您可以看到create_thread
模板在生成的转储中如何实例化:
$ grep create_thread 111.cpp.003t.original
;; Function void create_thread() [with int n = 0] (null)
create_thread<10> () >>>>>;
;; Function void create_thread() [with int n = 10] (null)
create_thread<9> () >>>>>;
;; Function void create_thread() [with int n = 9] (null)
create_thread<8> () >>>>>;
;; Function void create_thread() [with int n = 8] (null)
create_thread<7> () >>>>>;
;; Function void create_thread() [with int n = 7] (null)
create_thread<6> () >>>>>;
;; Function void create_thread() [with int n = 6] (null)
create_thread<5> () >>>>>;
;; Function void create_thread() [with int n = 5] (null)
create_thread<4> () >>>>>;
;; Function void create_thread() [with int n = 4] (null)
create_thread<3> () >>>>>;
;; Function void create_thread() [with int n = 3] (null)
create_thread<2> () >>>>>;
;; Function void create_thread() [with int n = 2] (null)
create_thread<1> () >>>>>;
;; Function void create_thread() [with int n = 1] (null)
create_thread<0> () >>>>>;
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 在已经使用Git的情况下减少编译时间
- C++常量数组的编译时间较长
- 编译时间文本到数字转换 (atoi)
- 在C++中执行 N 阶乘编译时间的 3 种不同/相同方法
- DLLexport 类模板实例(专用化),减少了仅标头模板库的编译时间
- 替换枚举以最大化编译时间检查的最佳方法
- 不同C++功能的编译时间
- 在预编译标头中实例化模板会缩短编译时间吗?
- 使用 SCons 提取每个编译单元的编译时间
- 将 lambda 函数转换为另一个编译单元中的普通函数会缩短编译时间吗?
- std :: Invoke_result_t编译时间语法错误
- 模板;constexpr;编译时间
- 如何实现声明功能-C 11,编译时间
- 如何减少编译时间:在包含未触及的头文件的情况下
- C++编译时间类型确定
- 特征:返回对具有编译时间尺寸检查的矩阵块的引用
- 在编译时间定义多个派生类
- 在编译时间创建查找表
- C 函数在编译时间返回类型