as_sexp and as_cpp are not easily extensible
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
调研方向
Start with the reproducer in cpp11test/src/test-as.cpp and inspect the conversion declarations included through cpp11.hpp. Compare the current as_cpp/as_sexp overload approach with the proposed custom_conversion trait. Done should mean custom types can provide conversions without pre-declaring overloads, while existing shared_ptr and vector conversions continue to work.
由索引模型根据 Issue 内容生成。
描述
Arrow defines as_cpp and as_sexp for shared_ptr and vectors of shared_ptr using external_pointer, since both of those are used throughout the arrow API. This is currently accomplished by declaring the overloads of as_sexp before inclusion of the cpp11 headers:
namespace cpp11 {
template <typename T>
SEXP as_sexp(const std::shared_ptr<T>& ptr);
template <typename T>
SEXP as_sexp(const std::vector<std::shared_ptr<T>>& vec);
} // namespace cpp11
#include <cpp11.hpp>
https://github.com/apache/arrow/pull/7819/files#diff-db94c392857c3bad4f5f69e86cde917bR24-R32
Without this pre-declaration, ADL fails (for example) when named_arg::operator= attempts to instantiate as_sexp(const shared_ptr<T>&).
Reproducer:
diff --git a/cpp11test/src/test-as.cpp b/cpp11test/src/test-as.cpp
index 76b4ee3..c8770a8 100644
--- a/cpp11test/src/test-as.cpp
+++ b/cpp11test/src/test-as.cpp
@@ -8,7 +8,39 @@
#include "Rcpp.h"
+namespace test {
+
+struct triple {
+ std::string arch, vendor, os;
+};
+
+} // namespace test
+
+namespace cpp11 {
+
+template <typename T>
+cpp11::enable_if_t<std::is_same<T, test::triple>::value, test::triple> as_cpp(SEXP from) {
+ cpp11::strings r{from};
+
+ if (r.size() == 3) {
+ return test::triple{.arch = r[0], .vendor = r[1], .os = r[2]};
+ }
+
+ stop("Expected string vector of length 3");
+}
+
+SEXP as_sexp(const test::triple& from) {
+ return cpp11::writable::strings({from.arch, from.vendor, from.os});
+}
+
+} // namespace cpp11
+
context("as_cpp-C++") {
+ test_that("as_cpp<custom type>(SEXP)") {
+ cpp11::writable::list(
+ {"fs"_nm = test::triple{.arch = "seven", .vendor = "ono", .os = "sendai"}});
+ }
+
test_that("as_cpp<integer>(INTSEXP)") {
SEXP r = PROTECT(Rf_allocVector(INTSXP, 1));
INTEGER(r)[0] = 42;
This could be resolved by providing a user-specializable trait for conversion, for example:
template <typename T, typename Enable = void>
struct custom_conversion;
template <typename T>
auto as_cpp(SEXP from) -> decltype(custom_conversion<T>::as_cpp(from)) {
return custom_conversion<T>::as_cpp(from);
}
template <typename T>
auto as_sexp(const T& from) -> decltype(custom_conversion<T>::as_sexp(from)) {
return custom_conversion<T>::as_sexp(from);
}
Which in the case of the reproducer above would be used like so:
namespace cpp11 {
template <>
struct custom_conversion<test::triple> {
static test::triple as_cpp(SEXP from) {
cpp11::strings r{from};
if (r.size() == 3) {
return test::triple{.arch = r[0], .vendor = r[1], .os = r[2]};
}
stop("Expected string vector of length 3");
}
static SEXP as_sexp(const test::triple& from) {
return cpp11::writable::strings({from.arch, from.vendor, from.os});
}
};
}
- 主要语言
- C++
- 星标
- 224
- 派生
- 52
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
r-lib/cpp11 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 35/100
-
难度 3/5 1-2 天 新手友好度 48/100
-
难度 5/5 一周以上 新手友好度 20/100
-
难度 3/5 1-2 天 新手友好度 42/100
-
难度 3/5 1-2 天 新手友好度 45/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
google/libultrahdr#485 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
godotengine/godot#123776 ·
-
bug
难度 1/5 1 小时以内 新手友好度 60/100
-
good first issue
难度 1/5 1 小时以内 新手友好度 90/100
-
good first issue
难度 2/5 1-3 小时 新手友好度 75/100
ros2/common_interfaces#344 ·