Skip to content

Instantly share code, notes, and snippets.

@flandr
Last active August 29, 2015 14:13
Show Gist options
  • Save flandr/b1a9d0f7c56b09848e96 to your computer and use it in GitHub Desktop.
Save flandr/b1a9d0f7c56b09848e96 to your computer and use it in GitHub Desktop.
Surprising behavior of std::function move constructor in Apple clang
#include <stdio.h>
#include <functional>
#include <utility>
struct Big {
char data[1024];
};
int main(int argc, char **argv) {
Big blob;
// This bind will trigger small object optimization
std::function<void()> little = std::bind([]() { printf("little\n"); });
// This bind will not
std::function<void()> big = std::bind([](Big const& b) {
printf("big %c\n", b.data[0]);
}, blob);
auto little_moved = std::move(little);
auto big_moved = std::move(big);
// After move, one expects the source std::function to be empty
// (boolean value false)
printf("Little empty: %d\n", !little);
printf("Little (moved) empty: %d\n", !little_moved);
printf("Big empty: %d\n", !big);
printf("Big (moved) empty: %d\n", !big_moved);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment