前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >clang -Xclang -ast-print -fsyntax-only main.cc > main-init.cc

clang -Xclang -ast-print -fsyntax-only main.cc > main-init.cc

作者头像
lexingsen
发布2022-02-25 08:50:07
2330
发布2022-02-25 08:50:07
举报
文章被收录于专栏:乐行僧的博客

分析C++泛型编程代码的利器,生成实例化后的代码!!!

如下面我们编写计算斐波那契数列的元函数:

代码语言:javascript
复制
template <int N>
struct Fib {
  enum {Value = Fib<N-1>::Value + Fib<N-2>::Value};
};

template<>
struct Fib<1> {
  enum {Value = 1};
};

template<>
struct Fib<2> {
  enum {Value = 1};
};

int main() {
  Fib<20>::Value;
  return 0;
}

鼠标指针放到Fib<20>::Value;上,观察编译期的结果:

在这里插入图片描述
在这里插入图片描述

使用clang工具生成实例化的模板文件:

命令格式:clang -Xclang -ast-print -fsyntax-only main.cc > main-init.cc 其中输入文件为main.cc 输出文件为main-init.cc

生成的文件:

代码语言:javascript
复制
main-init.cc

template <int N> struct Fib {
    enum  {
        Value = Fib<N - 1>::Value + Fib<N - 2>::Value
    };
};
template<> struct Fib<20> {
    enum  {
        Value = Fib<20 - 1>::Value + Fib<20 - 2>::Value
    };
};
template<> struct Fib<19> {
    enum  {
        Value = Fib<19 - 1>::Value + Fib<19 - 2>::Value
    };
};
template<> struct Fib<18> {
    enum  {
        Value = Fib<18 - 1>::Value + Fib<18 - 2>::Value
    };
};
template<> struct Fib<17> {
    enum  {
        Value = Fib<17 - 1>::Value + Fib<17 - 2>::Value
    };
};
template<> struct Fib<16> {
    enum  {
        Value = Fib<16 - 1>::Value + Fib<16 - 2>::Value
    };
};
template<> struct Fib<15> {
    enum  {
        Value = Fib<15 - 1>::Value + Fib<15 - 2>::Value
    };
};
template<> struct Fib<14> {
    enum  {
        Value = Fib<14 - 1>::Value + Fib<14 - 2>::Value
    };
};
template<> struct Fib<13> {
    enum  {
        Value = Fib<13 - 1>::Value + Fib<13 - 2>::Value
    };
};
template<> struct Fib<12> {
    enum  {
        Value = Fib<12 - 1>::Value + Fib<12 - 2>::Value
    };
};
template<> struct Fib<11> {
    enum  {
        Value = Fib<11 - 1>::Value + Fib<11 - 2>::Value
    };
};
template<> struct Fib<10> {
    enum  {
        Value = Fib<10 - 1>::Value + Fib<10 - 2>::Value
    };
};
template<> struct Fib<9> {
    enum  {
        Value = Fib<9 - 1>::Value + Fib<9 - 2>::Value
    };
};
template<> struct Fib<8> {
    enum  {
        Value = Fib<8 - 1>::Value + Fib<8 - 2>::Value
    };
};
template<> struct Fib<7> {
    enum  {
        Value = Fib<7 - 1>::Value + Fib<7 - 2>::Value
    };
};
template<> struct Fib<6> {
    enum  {
        Value = Fib<6 - 1>::Value + Fib<6 - 2>::Value
    };
};
template<> struct Fib<5> {
    enum  {
        Value = Fib<5 - 1>::Value + Fib<5 - 2>::Value
    };
};
template<> struct Fib<4> {
    enum  {
        Value = Fib<4 - 1>::Value + Fib<4 - 2>::Value
    };
};
template<> struct Fib<3> {
    enum  {
        Value = Fib<3 - 1>::Value + Fib<3 - 2>::Value
    };
};
template<> struct Fib<1> {
    enum  {
        Value = 1
    };
};
template<> struct Fib<2> {
    enum  {
        Value = 1
    };
};
int main() {
    Fib<20>::Value;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档