Skip to content

Commit f6534a3

Browse files
author
thirdwing
committed
use traits in Vector ctor
1 parent 495e49e commit f6534a3

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

inst/include/Rcpp/traits/traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct int2type { enum { value = I }; };
3939
#include <Rcpp/traits/same_type.h>
4040
#include <Rcpp/traits/enable_if.h>
4141
#include <Rcpp/traits/is_wide_string.h>
42+
#include <Rcpp/traits/is_arithmetic.h>
4243
#include <Rcpp/traits/char_type.h>
4344
#include <Rcpp/traits/named_object.h>
4445
#include <Rcpp/traits/is_convertible.h>

inst/include/Rcpp/vector/Vector.h

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,11 @@ class Vector :
118118
Storage::set__( Rf_allocVector( RTYPE, siz) ) ;
119119
std::generate( begin(), end(), gen );
120120
}
121-
122-
Vector( const R_xlen_t& size ) {
123-
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
124-
init() ;
125-
}
126-
127-
Vector( const size_t& size ) {
128-
Storage::set__( Rf_allocVector( RTYPE, static_cast<R_xlen_t>(size)) ) ;
129-
init() ;
130-
}
131-
Vector( const unsigned int& size ) {
132-
Storage::set__( Rf_allocVector( RTYPE, static_cast<R_xlen_t>(size)) ) ;
133-
init() ;
134-
}
135-
136-
Vector( const double& size ) {
137-
Storage::set__( Rf_allocVector( RTYPE, static_cast<R_xlen_t>(size)) ) ;
138-
init() ;
139-
}
140-
141-
Vector( const int& size ) {
121+
122+
// Add template class T and then restict T to arithmetic.
123+
template <typename T>
124+
Vector(T size,
125+
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
142126
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
143127
init() ;
144128
}

0 commit comments

Comments
 (0)