为什么 std::bitset::at() 抛出 out_of_range ?
这已经困扰了我几个小时,因为我在数学或代码中看不到任何问题。 (尽管盯着它并一遍又一遍地计算以确保确定。)我希望你们能帮助我,这是我的代码:
#define SOLVE_POSITION(x, y, z) ( z*16 + y*4 + x )
std::bitset<64> block;
block.reset();
for(int z = 0; z < 4; ++z){
for(int y = 0; y < 4; ++y){
for(int x = 0; x < 4; ++x){
if(block.at(SOLVE_POSITION(3-x, y, 3-z))){ //<-- call to at() throws 'out_of_range'
// do stuff
};
};
};
};
z
为 0,两个最内部的 for 循环完全运行他们的课程(总共 16 遍)。但是,一旦 z
变为 1,就会从 std::bitset<64>::at() 内部抛出异常。
z
、y
、x
的值分别为1
、0
、< code>0 那一刻。
你能告诉我这里发生了什么导致这个异常吗? 提前致谢!
This has stumped me for a few hours now, since I cannot see any problem in the math or code. (Dispite staring at it and working it out over and over again to be sure.) I'm hoping you folks can help me, here's my code:
#define SOLVE_POSITION(x, y, z) ( z*16 + y*4 + x )
std::bitset<64> block;
block.reset();
for(int z = 0; z < 4; ++z){
for(int y = 0; y < 4; ++y){
for(int x = 0; x < 4; ++x){
if(block.at(SOLVE_POSITION(3-x, y, 3-z))){ //<-- call to at() throws 'out_of_range'
// do stuff
};
};
};
};
With z
being 0, the two inner most for loops run their course entirely (for a total of 16 passes.) However, once z
becomes 1, that's when the exception is thrown from within std::bitset<64>::at().
The values of z
, y
, x
are respectively 1
, 0
, 0
at that moment.
Can you tell me what is happening here to cause this exception?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
宏!你必须非常小心这一点:
你定义了:
所以当你这样做时:
它会扩展为:
并且由于运算符优先级,
3-x*16
将是不正确的!您需要执行以下操作:以便它
按预期正确扩展为:。
Macros! You have to be really careful about this:
You define:
so when you do:
it expands to:
and because of operator precedence,
3-x*16
will be incorrect! You need to do:so that it expands correctly to:
as expected.
宏使用文本替换,您有效地告诉编译器
要解决此问题,请确保用括号将宏参数括起来:
Macros use text substitution, you're effectively telling the compiler
To fix this, make sure you surround your macro arguments with parenthesis: