Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Last active January 27, 2024 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/ea9f0707aaf25af66ca1f0b01b13a3f6 to your computer and use it in GitHub Desktop.
Save oconnor663/ea9f0707aaf25af66ca1f0b01b13a3f6 to your computer and use it in GitHub Desktop.
references in structs and temporary lifetime extension
#include <iostream>
struct Foo {
const int &x;
};
struct ConvertedFoo {
const int &x;
// Foo is implicitly convertible to ConvertedFoo
ConvertedFoo(const Foo &foo) : x(foo.x) {}
};
int main() {
// works
const Foo &foo42 = Foo{42};
std::cout << foo42.x;
// ASan error
// const ConvertedFoo &foo43 = Foo{43};
// std::cout << foo43.x;
// ASan error in Clang but not GCC
// auto foo44 = Foo{44};
// std::cout << foo44.x;
}
@oconnor663
Copy link
Author

oconnor663 commented Jan 27, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment