Skip to content

Commit dcd750d

Browse files
committed
alternate LogicalVector construction from bool
without using default template arguments, which is a C++11 feature. The handcrafted traits::is_bool may not cover all cases, but it works well for the constructor.
1 parent 000c1a6 commit dcd750d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

inst/include/Rcpp/traits/is_bool.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef Rcpp__traits__is_bool__h
2+
#define Rcpp__traits__is_bool__h
3+
4+
namespace Rcpp{ namespace traits {
5+
6+
template<typename>
7+
struct is_bool
8+
: public false_type { };
9+
10+
template<>
11+
struct is_bool<bool>
12+
: public true_type { };
13+
14+
template<>
15+
struct is_bool<const bool>
16+
: public true_type { };
17+
18+
template<>
19+
struct is_bool<volatile bool>
20+
: public true_type { };
21+
22+
}}
23+
24+
#endif

inst/include/Rcpp/traits/traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct int2type { enum { value = I }; };
6060
#include <Rcpp/traits/is_finite.h>
6161
#include <Rcpp/traits/is_infinite.h>
6262
#include <Rcpp/traits/is_nan.h>
63+
#include <Rcpp/traits/is_bool.h>
6364
#include <Rcpp/traits/if_.h>
6465
#include <Rcpp/traits/get_na.h>
6566
#include <Rcpp/traits/is_trivial.h>

inst/include/Rcpp/vector/Vector.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ class Vector :
120120
}
121121

122122
// Enable construction from bool for LogicalVectors
123-
template <int T = RTYPE>
124-
Vector(bool value, typename Rcpp::traits::enable_if<T == LGLSXP, void>::type* = 0) {
123+
// SFINAE only work for template. Add template class T and then restict T to
124+
// bool.
125+
template <typename T>
126+
Vector(T value,
127+
typename Rcpp::traits::enable_if<traits::is_bool<T>::value && RTYPE == LGLSXP, void>::type* = 0) {
125128
Storage::set__(Rf_allocVector(RTYPE, 1));
126129
fill(value);
127130
}

0 commit comments

Comments
 (0)