Skip to content

Commit

Permalink
Add function::call to conditionally call function, close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
dancazarin committed Sep 1, 2016
1 parent 84b4972 commit 6b8b9b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/cometa.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1484,9 +1484,11 @@ struct function<Result(Args...)>
fn = temp; fn = temp;
return *this; return *this;
} }
CMT_INTRIN Result operator()(Args... args) const CMT_INTRIN Result operator()(Args... args) const { return (*fn)(std::forward<Args>(args)...); }
template <typename TResult>
CMT_INTRIN Result call(TResult&& default_result, Args... args) const
{ {
return (*fn)(std::forward<Args>(args)...); return fn ? (*fn)(std::forward<Args>(args)...) : std::forward<TResult>(default_result);
} }
CMT_INTRIN explicit operator bool() const noexcept { return !!fn; } CMT_INTRIN explicit operator bool() const noexcept { return !!fn; }


Expand Down Expand Up @@ -1987,7 +1989,7 @@ constexpr size_t typename_postfix = sizeof("]") - 1;
template <size_t... indices, size_t Nout = 1 + sizeof...(indices)> template <size_t... indices, size_t Nout = 1 + sizeof...(indices)>
constexpr cstring<Nout> gettypename_impl(const char* str, csizes_t<indices...>) noexcept constexpr cstring<Nout> gettypename_impl(const char* str, csizes_t<indices...>) noexcept
{ {
return cstring<Nout>{{ (str[indices])..., 0 }}; return cstring<Nout>{ { (str[indices])..., 0 } };
} }
} }


Expand Down
4 changes: 4 additions & 0 deletions tests/cometa_test.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ int main(int, char**)
int& z = fn(x); int& z = fn(x);
(void)y; (void)y;
(void)z; (void)z;
function<int&(int&)> empty_fn;
int d;
int& a = fn.call(d, x);
(void)a;
} }


return 0; return 0;
Expand Down

0 comments on commit 6b8b9b7

Please sign in to comment.