Skip to content

Instantly share code, notes, and snippets.

@preshing
Created April 27, 2019 03:56
Show Gist options
  • Save preshing/561dca75964f284b376b1610b11735fd to your computer and use it in GitHub Desktop.
Save preshing/561dca75964f284b376b1610b11735fd to your computer and use it in GitHub Desktop.
Testing range-based for loop over explicitly created std::initializer_list
Results of a few quick tests performed in response to:
https://twitter.com/Steven_Pigeon/status/1121900020746338305
// This is the original version that HAS the bug (garbage value encountered during iteration):
for (const AxisRay& faceRelVedge : std::initializer_list<AxisRay>{
{{0, 0, -1}, Axis3::YNeg},
{{0, 1, 0}, Axis3::ZPos},
{{-2, -1, 0}, Axis3::ZPos},
{{-1, -1, -1}, Axis3::XPos}}) {
Topology::UsedVoxelFaceTraits::Key faceKey = {nextVedgeToWorld * (faceRelVedge.pos * (QStep / 2)), nextVedgeToWorld.rot * faceRelVedge.dir};
auto faceCursor = topo.usedVoxelFaces.findOrInsert(faceKey);
TURF_ASSERT(!faceCursor.wasFound());
}
// This version DOES NOT HAVE the bug:
std::initializer_list<AxisRay> tempList{
{{0, 0, -1}, Axis3::YNeg},
{{0, 1, 0}, Axis3::ZPos},
{{-2, -1, 0}, Axis3::ZPos},
{{-1, -1, -1}, Axis3::XPos}};
for (const AxisRay& faceRelVedge : tempList) {
Topology::UsedVoxelFaceTraits::Key faceKey = {nextVedgeToWorld * (faceRelVedge.pos * (QStep / 2)), nextVedgeToWorld.rot * faceRelVedge.dir};
auto faceCursor = topo.usedVoxelFaces.findOrInsert(faceKey);
TURF_ASSERT(!faceCursor.wasFound());
}
// This version DOES NOT HAVE the bug:
auto tempList = std::initializer_list<AxisRay>{
{{0, 0, -1}, Axis3::YNeg},
{{0, 1, 0}, Axis3::ZPos},
{{-2, -1, 0}, Axis3::ZPos},
{{-1, -1, -1}, Axis3::XPos}};
for (const AxisRay& faceRelVedge : tempList) {
Topology::UsedVoxelFaceTraits::Key faceKey = {nextVedgeToWorld * (faceRelVedge.pos * (QStep / 2)), nextVedgeToWorld.rot * faceRelVedge.dir};
auto faceCursor = topo.usedVoxelFaces.findOrInsert(faceKey);
TURF_ASSERT(!faceCursor.wasFound());
}
// This version HAS the bug (garbage value again encountered during iteration):
auto&& tempList = std::initializer_list<AxisRay>{
{{0, 0, -1}, Axis3::YNeg},
{{0, 1, 0}, Axis3::ZPos},
{{-2, -1, 0}, Axis3::ZPos},
{{-1, -1, -1}, Axis3::XPos}};
for (const AxisRay& faceRelVedge : tempList) {
Topology::UsedVoxelFaceTraits::Key faceKey = {nextVedgeToWorld * (faceRelVedge.pos * (QStep / 2)), nextVedgeToWorld.rot * faceRelVedge.dir};
auto faceCursor = topo.usedVoxelFaces.findOrInsert(faceKey);
TURF_ASSERT(!faceCursor.wasFound());
}
// This version HAS the bug (garbage value again encountered during iteration):
const auto& tempList = std::initializer_list<AxisRay>{
{{0, 0, -1}, Axis3::YNeg},
{{0, 1, 0}, Axis3::ZPos},
{{-2, -1, 0}, Axis3::ZPos},
{{-1, -1, -1}, Axis3::XPos}};
for (const AxisRay& faceRelVedge : tempList) {
Topology::UsedVoxelFaceTraits::Key faceKey = {nextVedgeToWorld * (faceRelVedge.pos * (QStep / 2)), nextVedgeToWorld.rot * faceRelVedge.dir};
auto faceCursor = topo.usedVoxelFaces.findOrInsert(faceKey);
TURF_ASSERT(!faceCursor.wasFound());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment