Skip to content

Instantly share code, notes, and snippets.

@ecatmur
Last active July 6, 2022 20:30
Show Gist options
  • Save ecatmur/e6cfe210af3d3b45926d946f226cadfc to your computer and use it in GitHub Desktop.
Save ecatmur/e6cfe210af3d3b45926d946f226cadfc to your computer and use it in GitHub Desktop.
piecewise-construct-ub
$ g++ --version
g++ (GCC) 12.1.0
$ g++ -O2 -std=c++14 a.cpp main.cpp && ./a.out
a.out: main.cpp:5: int main(): Assertion `f() == (*c().target<F*>())()' failed.
Aborted
$ clang++ --version
clang version 14.0.5
$ clang++ -O2 -std=c++14 a.cpp main.cpp && ./a.out
a.out: main.cpp:5: int main(): Assertion `f() == (*c().target<F*>())()' failed.
Aborted
#include "odr.hpp"
std::function<F> c() { return f; }
#include "odr.hpp"
#include <cassert>
int main() {
assert(f == *c().target<F*>()); // passes
assert(f() == (*c().target<F*>())()); // fails
}
#include <utility>
#include <functional>
inline void const* f() { return &std::piecewise_construct; }
using F = void const*();
std::function<F> c();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment