Skip to content

Instantly share code, notes, and snippets.

@mkmkme
Created November 26, 2021 11:20
Show Gist options
  • Save mkmkme/da90c5df141e66c17c7ea9b0b27c25b3 to your computer and use it in GitHub Desktop.
Save mkmkme/da90c5df141e66c17c7ea9b0b27c25b3 to your computer and use it in GitHub Desktop.
auto return type
#include <optional>
#include <iostream>
auto foo(int x)
{
if (x > 5)
return std::optional<int>{x};
else
return std::nullopt;
}
int main()
{
std::cout << foo(3).has_value() << ' ' << foo(7).has_value() << std::endl;
return 0;
}
~
❯ g++ autohell.cpp
autohell.cpp: In function ‘auto foo(int)’:
autohell.cpp:10:21: error: inconsistent deduction for auto return type: ‘std::optional<int>’ and then ‘std::nullopt_t’
10 | return std::nullopt;
| ~~~~~^~~~~~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment