
template<int x , int y> void printInfo(); template<int x , int y> void printInfo(){ std::cout<<(x + y); } //---- printInfo<1,2>(); 对于这样一个函数调用,x+y 是在编译器计算的,还是在运行期间计算的?
1 fengjianxinghun 2021-01-25 11:18:07 +08:00 1. 这算不上元编程。 2. x+y 开优化的情况是编译期,常量传播。 |
2 no1xsyzy 2021-01-25 12:21:21 +08:00 gcc 10.2 -O0 也是编译期完成计算 https://godbolt.org/z/3dbYhW 开优化会 inline (但不确定是否必然 inline ) 但考虑到 + 可以被重载,这应当被视为 int 特性而不是模板特性。 顺便来点诡异的: https://godbolt.org/z/xrrcqv |
4 James369 2021-01-26 10:02:36 +08:00 你的代码怎么格式化的这么漂亮,怎么发贴含代码的? |
5 QBugHunter OP @James369 mark down 语法。。。。。 |