`const_string_proxy` comparisons are ambiguous with Apple clang (non-const `operator==` / `operator!=`)

未关闭 适合新手
#1,537 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
1/5
预计耗时
1 小时以内
新手友好度
90/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
r
领域
backend

调研方向

从 inst/include/Rcpp/vector/const_string_proxy.h 开始,其中四个非 const 的 operator== 和 operator!= 重载与 string_proxy.h 中对应的运算符不同。使用 Apple clang 14 和 17,在 C++17 和 C++20 模式下编译并运行该 issue 中的 Rcpp::export 示例。完成的标准是 issue 中展示的所有 const_Proxy 比较都能编译通过,不出现重载歧义错误,并且保持现有的比较行为。

由索引模型根据 Issue 内容生成。

描述

Comparing two CharacterVector::const_Proxy values does not compile with Apple clang 14 in C++20 mode, and
comparing a const_Proxy with a SEXP does not compile with Apple clang 14 or 17.

#include <Rcpp.h>

// [[Rcpp::export]]
bool same(const Rcpp::CharacterVector x) {
    Rcpp::CharacterVector::const_Proxy a = x[0], b = x[1];
    return a == b;                                          // ambiguous: clang 14, C++20
}

// [[Rcpp::export]]
bool sx(const Rcpp::CharacterVector x, SEXP s) {
    Rcpp::CharacterVector::const_Proxy a = x[0];
    return a == s || a != s;                                // ambiguous: clang 14 and 17, C++17 and C++20
}
error: use of overloaded operator '==' is ambiguous (with operand types
  'Rcpp::CharacterVector::const_Proxy' (aka 'const_string_proxy<16, Rcpp::PreserveStorage>') and
  'Rcpp::CharacterVector::const_Proxy')

In inst/include/Rcpp/vector/const_string_proxy.h the const char* and const const_string_proxy&
overloads of operator== and operator!= are not declared const, while the SEXP overloads and all the corresponding operators in string_proxy.h, are.

Adding const to those four operators resolves all of the above (also in PR):

@@ -97,17 +97,17 @@ namespace internal{
 		inline bool empty() const { return *begin() == '\0' ; }
 		inline reference operator[]( R_xlen_t n ){ return *( begin() + n ) ; }
 
-		bool operator==( const char* other){
+		bool operator==( const char* other) const {
 			return strcmp( begin(), other ) == 0 ;
 		}
-		bool operator!=( const char* other){
+		bool operator!=( const char* other) const {
 			return strcmp( begin(), other ) != 0 ;
 		}
 
-		bool operator==( const const_string_proxy& other){
+		bool operator==( const const_string_proxy& other) const {
 			return strcmp( begin(), other.begin() ) == 0 ;
 		}
-		bool operator!=( const const_string_proxy& other){
+		bool operator!=( const const_string_proxy& other) const {
 			return strcmp( begin(), other.begin() ) != 0 ;
 		}
 

Affected CRAN packages that fail to install for this reason: comparator, IFC, IsoSpecR,
packcircles, runner.

Seen with R 4.6.1, Rcpp 1.1.2, Apple clang 14.0.3. The operators are unchanged in current Rcpp master (279801e, 1.1.2.8), where Apple clang 17.0.0 (arm64) gives the same SEXP errors but accepts same(). With the changes above the example compiles in both modes with both compilers.

主要语言
C++
星标
796
派生
217
平均合并
1 天 12 小时
30 天内合并 PR
2

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RcppCore/Rcpp 的其他 Issue

查看 RcppCore/Rcpp 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。