为什么以下C++元编程代码在 N=100 时给出编译错误,但对于小 N 运行良好
Why does following C++ meta-programming code gives compilation error for N=100 but runs fine for small N?
#include<math.h>
#include<iostream>
using namespace std;
template <int x, int N> struct f{
static const int res = x*(f<x, N-1>::res);
};
template <int x> struct f<x, 0>{
static const int res = 1;
};
int main(){
for(int i=0;i<1000000;++i)
int a = (f<3, 100>::res);
}
如果我将 f<3, 100> 更改为 f<3, 10>代码运行正常。但是对于 100,它会给出以下错误:
.
.
.
k.cpp:7:26: recursively required from ‘const int f<3, 99>::res’
k.cpp:7:26: required from ‘const int f<3, 100>::res’
k.cpp:16:23: required from here
k.cpp:7:19: error: initializer invalid for static member with constructor
k.cpp:7:19: error: ‘f<3, 98>::res’ cannot be initialized by a non-constant expression when being declared
k.cpp: In instantiation of ‘const int f<3, 99>::res’:
k.cpp:7:26: required from ‘const int f<3, 100>::res’
k.cpp:16:23: required from here
k.cpp:7:19: error: initializer invalid for static member with constructor
k.cpp:7:19: error: ‘f<3, 99>::res’ cannot be initialized by a non-constant expression when being declared
k.cpp: In instantiation of ‘const int f<3, 100>::res’:
k.cpp:16:23: required from here
k.cpp:7:19: error: initializer invalid for static member with constructor
k.cpp:7:19: error: ‘f<3, 100>::res’ cannot be initialized by a non-constant expression when being declared
我是元编程的新手。虽然错误看起来像与const
有关的东西,但事实并非如此。有人可以帮我理解吗?
启用警告并仔细阅读错误输出:
warning: integer overflow in expression [-Woverflow]
static const int res = x*(f<x, N-1>::res);
~^~~~~~~~~~~~~~~~~
error: overflow in constant expression [-fpermissive]
static const int res = x*(f<x, N-1>::res);
^~~
对于足够大的N
值,您将有一个有符号整数溢出,这是运行时上下文中的未定义行为,并在常量表达式中产生编译时错误。
如果我尝试int a = (f<3, 20>::res);
我会得到
warning C4307: '*': integral constant overflow
将int
作为long long
可以克服这一点,并为f<3, 20>
提供3486784401。
这可以应对高达f<3, 39>
4052555153018976267然后溢出。
道德:警告是有帮助的。
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译