Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/maybe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ struct MaybeIfImpl {


template <typename M, typename Functor>
auto maybe_if(M& m, Functor function)
auto maybe_if(M&& m, Functor function)
-> typename MaybeIfImpl<M, decltype(function(m.infer_value_type()))>::ResultType
{
return MaybeIfImpl<M, decltype(function(m.infer_value_type()))>::maybe_if(m, function);
}

template <typename M, typename Functor>
auto maybe_if(M& m, Functor function)
auto maybe_if(M&& m, Functor function)
-> typename MaybeIfImpl<M, decltype(function(*m))>::ResultType
{
return MaybeIfImpl<M, decltype(function(*m))>::maybe_if(m, function);
Expand Down
6 changes: 3 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ CXXFLAGS = -O0 -g -I.. -std=c++11 -stdlib=libc++ -Wall -Werror -Wno-unused
CXX = clang++
COMPILE = $(CXX) $(CXXFLAGS)

maybe_test: maybe_test.cpp ../maybe.hpp
maybe_test: maybe_test.cpp ../base/maybe.hpp
$(COMPILE) -o maybe_test maybe_test.cpp

test:
test: maybe_test
./maybe_test

clean:
rm -f maybe_test

all: maybe_test
all: maybe_test
14 changes: 12 additions & 2 deletions test/maybe_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "maybe.hpp"
#include "../base/maybe.hpp"

#if defined(__has_feature) && __has_feature(cxx_lambdas)
#define HAS_LAMBDAS 1
Expand Down Expand Up @@ -271,6 +271,15 @@ void test_maybe_if() {
m = maybe_if(p, [](int it) { return it; });
ASSERT(!m);
}

// Test rvalue Maybe<>
{
int n = 123;
int observed = 0;
auto m = maybe_if(Maybe<int>(123), [&](int it) { observed = it; });
ASSERT(m);
ASSERT(observed == 123);
}
}

int main (int argc, char const *argv[])
Expand Down Expand Up @@ -375,5 +384,6 @@ int main (int argc, char const *argv[])

test_maybe_if();

printf("All tests passed\n");
return 0;
}
}