TartanLlama / optional

C++11/14/17 std::optional with functional-style extensions and reference support
https://tl.tartanllama.xyz
Creative Commons Zero v1.0 Universal
859 stars 67 forks source link

`.map` with a callable returning a reference should return an optional reference #14

Closed TartanLlama closed 6 years ago

TartanLlama commented 6 years ago

Thanks to @ThePhD.

struct foo{
    int& v() { return i; }
    int i = 0;
};

int& x(int& i) { i = 42; return i;}

int main() {
    tl::optional<foo> f = foo{};
    auto v = f.map(&foo::v).map(x);
    static_assert(std::is_same_v<decltype(v), tl::optional<int&>>);
    //f.i should be 42
}