Skip to content

capture parameter pack as a tuple to work around gcc4.8 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 5, 2020
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cpp11 (development version)

* cpp11 is now able to compile on gcc 4.8.5 (#69, @bkietz)

* `as_cpp<E>()` now allows enumeration `E` (#52, @bkietz)

* `writable::logicals::operator=()` now allows C++ boolean values (#57, @romainfrancois)
Expand Down
54 changes: 50 additions & 4 deletions inst/include/cpp11/protect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <exception> // for exception
#include <stdexcept> // for std::runtime_error
#include <string> // for string, basic_string
#include <tuple> // for tuple, make_tuple
#include "R_ext/Error.h" // for Rf_error, Rf_warning
#include "R_ext/Print.h" // for REprintf
#include "R_ext/Utils.h" // for R_CheckUserInterrupt
Expand Down Expand Up @@ -177,12 +178,57 @@ void unwind_protect(Fun code) {
}
#endif

namespace detail {

template <size_t...>
struct index_sequence {
using type = index_sequence;
};

template <typename, size_t>
struct appended_sequence;

template <std::size_t... I, std::size_t J>
struct appended_sequence<index_sequence<I...>, J> : index_sequence<I..., J> {};

template <size_t N>
struct make_index_sequence
: appended_sequence<typename make_index_sequence<N - 1>::type, N - 1> {};

template <>
struct make_index_sequence<0> : index_sequence<> {};

template <typename F, typename... A, size_t... I>
auto apply(F&& f, std::tuple<A...>&& a, const index_sequence<I...>&)
-> decltype(f(std::get<I>(std::move(a))...)) {
return f(std::get<I>(std::move(a))...);
}

template <typename F, typename... A>
auto apply(F&& f, std::tuple<A...>&& a)
-> decltype(apply(f, std::move(a), make_index_sequence<sizeof...(A)>{})) {
return apply(f, std::move(a), make_index_sequence<sizeof...(A)>{});
}

// overload to silence a compiler warning that the tuple parameter is set but unused
template <typename F>
auto apply(F&& f, std::tuple<> &&) -> decltype(f()) {
return f();
}

} // namespace detail

struct protect {
template <typename F>
struct function {
template <typename... A>
auto operator()(A... a) const -> decltype(std::declval<F*>()(a...)) {
return unwind_protect([&] { return ptr_(a...); });
auto operator()(A&&... a) const
-> decltype(detail::apply(std::declval<F*>(),
std::forward_as_tuple(std::forward<A>(a)...))) {
// workaround to support gcc4.8, which can't capture a parameter pack
auto a_packed_refs = std::forward_as_tuple(std::forward<A>(a)...);
return unwind_protect(
[&] { return detail::apply(ptr_, std::move(a_packed_refs)); });
}
F* ptr_;
};
Expand All @@ -198,15 +244,15 @@ inline void check_user_interrupt() { safe[R_CheckUserInterrupt](); }

template <typename... Args>
void stop [[noreturn]] (const char* fmt, Args... args) {
unwind_protect([&] { Rf_error(fmt, args...); });
safe[Rf_error](fmt, args...);
// Compiler hint to allow [[noreturn]] attribute; this is never executed since Rf_error
// will longjmp
throw std::runtime_error("stop()");
}

template <typename... Args>
void stop [[noreturn]] (const std::string& fmt, Args... args) {
unwind_protect([&] { Rf_error(fmt.c_str(), args...); });
safe[Rf_error](fmt.c_str(), args...);
// Compiler hint to allow [[noreturn]] attribute; this is never executed since Rf_error
// will longjmp
throw std::runtime_error("stop()");
Expand Down