Skip to content

Commit 4018bb8

Browse files
committed
Disable comma initialization (hide it behind #define); update unit tests
2 parents 732856e + 6a15a10 commit 4018bb8

File tree

6 files changed

+96
-24
lines changed

6 files changed

+96
-24
lines changed

inst/NEWS.Rd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
\code{IntegerVector}s (0-based index subsetting). Such subsetting
2727
will also work with Rcpp sugar expressions, enabling expressions
2828
such as \code{x[ x > 0]}.
29+
\item Comma initialization (e.g.
30+
\code{CharacterVector x = "a", "b", "c";}, has been disabled, as
31+
it causes problems with the behavior of the \code{=} operator with
32+
\code{Rcpp::List}s. Users who want to re-enable this functionality
33+
can use \code{#define RCPP_COMMA_INITIALIZATION}, but be aware of
34+
the above caveat. The more verbose
35+
\code{CharacterVector x = CharacterVector::create("a", "b", "c")}
36+
is preferred.
2937
}
3038
\item Changes in Rcpp Attributes
3139
\itemize{

inst/include/Rcpp/vector/Vector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ class Vector :
213213
return traits::is_na<RTYPE>(x);
214214
}
215215

216+
#ifdef RCPP_COMMA_INITIALIZATION
216217
internal::ListInitialization<iterator,init_type> operator=( init_type x){
217218
iterator start = begin() ; *start = x;
218219
return internal::ListInitialization<iterator,init_type>( start + 1 ) ; ;
219220
}
221+
#endif
220222

221223
/**
222224
* the length of the vector, uses Rf_length

inst/unitTests/cpp/Vector.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,6 @@ int integer_names_indexing( IntegerVector y ){
186186
return y["foo"] ;
187187
}
188188

189-
// [[Rcpp::export]]
190-
IntegerVector integer_comma(){
191-
IntegerVector x(4) ;
192-
x = 0, 1, 2, 3 ;
193-
return x ;
194-
}
195-
196189
// [[Rcpp::export]]
197190
IntegerVector integer_push_back( IntegerVector y ){
198191
y.push_back( 5 ) ;
@@ -599,13 +592,6 @@ std::string character_names_indexing( CharacterVector y ){
599592
return foo ;
600593
}
601594

602-
// [[Rcpp::export]]
603-
CharacterVector character_comma(){
604-
CharacterVector x(3) ;
605-
x = "foo", "bar", "bling" ;
606-
return x ;
607-
}
608-
609595
// [[Rcpp::export]]
610596
List character_listOf( List ll ){
611597
CharacterVector cv1 = ll["foo"];
@@ -757,3 +743,10 @@ CharacterVector sort_character(CharacterVector x) {
757743
LogicalVector sort_logical(LogicalVector x) {
758744
return x.sort();
759745
}
746+
747+
// [[Rcpp::export]]
748+
List list_sexp_assign(SEXP x) {
749+
List L;
750+
L = x;
751+
return L;
752+
}

inst/unitTests/cpp/VectorOld.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// Vector-old.cpp: Rcpp R/C++ interface class library -- Vector unit tests
4+
//
5+
// Copyright (C) 2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#define RCPP_COMMA_INITIALIZATION
23+
#include <Rcpp.h>
24+
using namespace Rcpp;
25+
26+
// [[Rcpp::export]]
27+
IntegerVector integer_comma(){
28+
IntegerVector x(4) ;
29+
x = 0, 1, 2, 3 ;
30+
return x ;
31+
}
32+
33+
// [[Rcpp::export]]
34+
CharacterVector character_comma(){
35+
CharacterVector x(3) ;
36+
x = "foo", "bar", "bling" ;
37+
return x ;
38+
}

inst/unitTests/runit.Vector.R

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,6 @@ if (.runThisTest) {
152152
checkEquals( fun( x ), 1L, msg = "IntegerVector names based indexing" )
153153
}
154154

155-
test.IntegerVector.comma <- function(){
156-
fun <- integer_comma
157-
checkEquals( fun(), 0:3, msg = "IntegerVector comma initialization" )
158-
}
159-
160155
test.IntegerVector.push.back <- function(){
161156
fun <- integer_push_back
162157
checkEquals( fun(1:4), 1:5, msg = "IntegerVector push back" )
@@ -520,11 +515,6 @@ if (.runThisTest) {
520515
checkEquals( fun(x), "foo", msg = "CharacterVector names based indexing" )
521516
}
522517

523-
test.CharacterVector.comma <- function(){
524-
fun <- character_comma
525-
checkEquals( fun(), c("foo","bar", "bling" ), msg = "CharacterVector comma operator" )
526-
}
527-
528518
test.CharacterVector.listOf <- function() {
529519
fun <- character_listOf
530520
checkEquals(fun(list(foo=c("tic","tac","toe"),
@@ -669,5 +659,12 @@ if (.runThisTest) {
669659
lgcl <- as.logical(int)
670660
checkIdentical( sort_logical(lgcl), sort(lgcl, na.last=TRUE) )
671661
}
662+
663+
test.List.assign.SEXP <- function() {
664+
l <- list(1, 2, 3)
665+
other <- list_sexp_assign(l)
666+
checkIdentical(l, other)
667+
}
668+
672669
}
673670

inst/unitTests/runit.VectorOld.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (C) 2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey
2+
#
3+
# This file is part of Rcpp.
4+
#
5+
# Rcpp is free software: you can redistribute it and/or modify it
6+
# under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Rcpp is distributed in the hope that it will be useful, but
11+
# WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17+
18+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
19+
20+
if (.runThisTest) {
21+
22+
.setUp <- Rcpp:::unitTestSetup("VectorOld.cpp")
23+
24+
test.IntegerVector.comma <- function(){
25+
fun <- integer_comma
26+
checkEquals( fun(), 0:3, msg = "IntegerVector comma initialization" )
27+
}
28+
29+
test.CharacterVector.comma <- function(){
30+
fun <- character_comma
31+
checkEquals( fun(), c("foo","bar", "bling" ), msg = "CharacterVector comma operator" )
32+
}
33+
34+
}

0 commit comments

Comments
 (0)