Skip to content

All uses auto&& in function signatures potentially undefined behavior #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kelbon opened this issue Dec 22, 2022 · 2 comments
Closed

Comments

@kelbon
Copy link

kelbon commented Dec 22, 2022

constexpr auto operator_as( std::variant<Ts...> const& x ) -> auto&& {

Its not equal to decltype(auto).
If returns by value, then auto&& generates dangling rvalue reference, while decltype(auto) returns by value as needed

https://godbolt.org/z/7Gfr5fb1d

template< typename C, typename X >
    requires std::is_same_v<C, X>
auto as( X const& x ) -> auto&& {
    return 5;
}

int main() {
    static_assert(std::is_same_v<decltype(as<int>(5)), int&&>);
}
@kelbon kelbon changed the title All uses auto&& in signatures possibly undefined behavior All uses auto&& in function signatures potentially undefined behavior Dec 22, 2022
@JohelEGP
Copy link
Contributor

@hsutter
Copy link
Owner

hsutter commented Dec 22, 2022

Good catch, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants