Last active
January 27, 2024 20:03
-
-
Save oconnor663/ea9f0707aaf25af66ca1f0b01b13a3f6 to your computer and use it in GitHub Desktop.
references in structs and temporary lifetime extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://godbolt.org/z/eb9WEPzMP