Permalink
Browse files

Add function::call to conditionally call function, close #1

  • Loading branch information...
dlevin256 committed Sep 1, 2016
1 parent 84b4972 commit 6b8b9b718aad826f3d9ebf418d7b290058166a67
Showing with 9 additions and 3 deletions.
  1. +5 −3 include/cometa.hpp
  2. +4 −0 tests/cometa_test.cpp
@@ -1484,9 +1484,11 @@ struct function<Result(Args...)>
fn = temp;
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; }

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

@@ -99,6 +99,10 @@ int main(int, char**)
int& z = fn(x);
(void)y;
(void)z;
function<int&(int&)> empty_fn;
int d;
int& a = fn.call(d, x);
(void)a;
}

return 0;

0 comments on commit 6b8b9b7

Please sign in to comment.