- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
带位掩码的数组真的很流行,通常它们写起来很乏味,而且它们使代码的可读性降低,我想用 constexpr
生成它们,这是我的尝试
#include <iostream>
#include <cstdint>
#include <vector>
#include <utility>
typedef uint32_t myT;
template <typename T>
constexpr std::vector<T> vecFarm(T &&lower, T &&upper, T &&step) {
// std::cout << lower << " " << upper << " " << step << "\n";
std::vector<T> v;
if (lower < upper) {
for (T count = lower; count < upper; count += step) {
v.push_back(count);
};
}
return (v);
}
int main() {
std::vector<myT> k(std::move(vecFarm(myT(0), ~(myT(0)), myT(256)))); //why
// this doesn't work ?
// std::vector<myT> k(std::move(vecFarm(myT(0), ((~(myT(0))) >> 16), myT(256))));
// but this one works
// let's see what we got
for (const auto &j : k) {
std::cout << j << " ";
}
std::cout << "\n";
return (0);
}
我使用了std::move
、未命名的对象和一个constexpr
,这段代码可以很好地编译
g++-4.8 -O3 -std=c++11 -pthread -Werror -Wall -Wextra
但由于 bad_alloc
,它在运行时失败了,我可以看到我的“小”应用程序分配了大量空间。
也许错误很大,我看不到,但为什么这不起作用?
为什么我的应用程序在运行时进行分配?不应该在编译时计算所有内容吗?我原以为这可能会在编译时失败,而不是在运行时。
最佳答案
std::bad_alloc
通常意味着它不能分配更多的内存。将您的代码更改为以下内容将告诉您原因:
for (T count = lower; count < upper; count += step) {
std::cout << "count:" << count << "\n";
std::cout << "upper:" << upper << "\n";
};
当我测试它时,这会在第一个循环中打印以下内容:
count:0
upper:4294967295
也就是说,在count < upper
之前你还有很长的路要走失败并且 for 循环停止,特别是因为您只添加了 256
每次。
此外,为了 constexpr
要在编译时评估的函数,有某些 conditions it has to fullfil .例如,its return type must be LiteralType
, 你的函数返回 std::vector
, 还有, exactly one return statement that contains only literal values, constexpr variables and functions.
你有一个复合语句。因此,您的函数无法在编译时求值。
另请注意,如果您不满足这些条件,constexpr
限定符将被忽略,尽管如果您打开 -pedantic
它应该可以为您提供更好的诊断。
关于C++11 - constexpr 上的 bad_alloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20028143/
这个错误的原因是什么?其实我不想受到之前测试用例结果的影响所以在测试用例开始时,我清空了队列以便每个测试用例都可以重新开始。 " #include #include using name
我的程序中出现了 bad_alloc 异常。 这些是限制条件: 1 #include #include #include class SuffixArray { std::vector
我在 digital ocean 上使用 node.js 并尝试运行文件上传/下载服务器。 为了确保服务器在后台运行并且不会因错误而退出,我使用了以下内容 nohup nodejs server.js
[前注:我已经阅读了 StackOverflow 中的现有线程。我的问题似乎没有] 我正在研究 Quake 2 MD2 格式。在新建一个指针数组后,我似乎得到了错误的分配。但是,如果我做一些可怕的指针
使用QAudioOut我试图按顺序播放存储在QByteArray中的数据...这在追加少量数据时起作用,但是当数据过多时,可以说是2到3个小时的RAW PCM从不同组合追加由于堆的大小不足以同时保存所
我是C++的新手,我真的很想使用Botan连接到硬件加密 token 。我不知道是否错过了libs或dlls的任何设置。 我基于Building Botan library in Windows 10
我正在用 C++ 编写一些使用 BFS 搜索迷宫的代码(我的主要语言是 Python,但我想稍微锻炼一下我的 C++ 大脑...),我偶然发现了这个奇怪的错误。 这里是相关的数据结构: struct
我必须为我的单元测试导致 bad_alloc(基本上,为了 100% 的代码覆盖率,我无法更改某些函数)。我该怎么办? 这是我的代码示例。我必须在这里的某个地方引起 bad_alloc。 bool i
在 c++ 中,分配内存时会发生 bad_alloc,或者至少我是这么理解的,但现在我在从数组读取内存时遇到此错误。我创建数组: int * possible = new int[ suma ]; 然
我遇到编译器错误: 错误 C2061:语法错误:标识符“bad_alloc” 我以前在其他项目中使用过下面的代码块,在 try block 中使用不同的内存分配,没有问题。我希望有人能向我解释为什么
我使用了这个伪代码: h := 1 /* Initialization of the pivot row */ k := 1 /* Initialization of the pivot colu
我四处寻找这个问题的解决方案,但找不到,所以我只好问问。我的程序是一个迷宫游戏,其中包含许多不同的房间,每个房间都使用指针链接到一个或多个相邻的房间。玩家从一个房间导航到另一个房间,直到找到导出。 当
我正在尝试递归地 segmentation 球体以在面上实现球面小波算法。虽然我有 8 GB 的可用内存,但我只能将二十面体 segmentation 为 9 个级别。我的问题是在构建我的球体时内存不
我从事一个游戏项目。最近,我们遇到了一个问题,当我们加载/卸载不同的场景大约 3 次时,我们捕获了“bad_alloc”异常。 每次加载场景时,我们首先将压缩的 .zip 文件夹加载到内存中,然后从中
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
当构造函数中抛出bad_alloc异常时,其中创建了多个对象,必须做些什么来清理内存。例如。 class Object { private: A * a; B * b;
我的程序抛出一个 std::bad_alloc。调试后发现是代码中抛出的 curFinalBucket->points.push_back(p); 其中点是一个 vector .在代码行之前,curF
我目前正在做一个 C++ 项目,现在我已经卡住了一段时间了。这是关于使用表达式模板和(至少对我来说)一个奇怪的 bad_alloc 的延迟评估。 如果您尝试下面的代码,您会注意到由于最后一次添加 b+
所以我有一个结构 struct float3 { float x, y, z; }; 我正在尝试创建一个函数来获取 x、y、z 值并将它们映射到键 0、1、2 以对应其各自的维度。我写了下面的
考虑这个简单的程序: #include #include int main(void) { const std::size_t size = 1 size_t_max / sizeof(i
我是一名优秀的程序员,十分优秀!