Skip to content

CharacterVector::const_iterator isn't const #228

@dcdillon

Description

@dcdillon

The following should not compile:

#include <Rcpp.h>
#include <Rcpp/String.h>

using namespace Rcpp ;

// [[Rcpp::export]]
CharacterVector character_iterator1( const CharacterVector letters ){
    std::string res ;
    CharacterVector::const_iterator first = letters.begin() ;
    CharacterVector::const_iterator last = letters.end() ;
    while( first != last ){
        *first = String("dan");
        ++first ;
    }
    return letters ;
}

// R code
# library(Rcpp11)
# library(attributes)
#
# sourceCpp("test.cpp")
#
# v <- letters
# result <- character_iterator1(v)
# result
# v

Currently it not only compiles, but actually modifies the Vector as evidenced in the output below.

 [1] "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan"
[13] "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan"
[25] "dan" "dan"
 [1] "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan"
[13] "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan" "dan"
[25] "dan" "dan"

Sorry for the repeated use of my name, but it's my go-to meaningless string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions