Skip to content

Sugar operator+ and SubsetProxy #392

@nathan-russell

Description

@nathan-russell

The following fails to compile (x86_64-redhat-linux, g++ 4.8.3, R 3.2.2),

#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector SubsetAdd(Rcpp::NumericVector lhs, Rcpp::NumericVector rhs) {
    Rcpp::LogicalVector idx = !Rcpp::is_na(lhs) & !Rcpp::is_na(rhs);
    return lhs[idx] + rhs[idx];        // error
} 

with error

no match for ‘operator+’ (operand types are 
‘Rcpp::SubsetProxy<14, Rcpp::PreserveStorage, 10, true, Rcpp::Vector<10, Rcpp::PreserveStorage> >’ 
and 
‘Rcpp::SubsetProxy<14, Rcpp::PreserveStorage, 10, true, Rcpp::Vector<10, Rcpp::PreserveStorage> >’)

This compiles and evaluates correctly:

Rcpp::NumericVector SubsetAdd(Rcpp::NumericVector lhs, Rcpp::NumericVector rhs) {
    Rcpp::LogicalVector idx = !Rcpp::is_na(lhs) & !Rcpp::is_na(rhs);

    return static_cast<const Rcpp::NumericVector&>(lhs[idx]) + 
        static_cast<const Rcpp::NumericVector&>(rhs[idx]);           // Ok
}

but surely is not ideal. I was unsuccessful in coming up with a fix for this, but hopefully someone can point me in the direction of a clever solution? (Presumably this functionality is desirable & possible.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions