* Create a range from two iterators, with type deduction.
*/
template <class Iter>
-Range<Iter> range(Iter first, Iter last) {
+constexpr Range<Iter> range(Iter first, Iter last) {
return Range<Iter>(first, last);
}
* Creates a range to reference the contents of a contiguous-storage container.
*/
// Use pointers for types with '.data()' member
-template <class Collection,
- class T = typename std::remove_pointer<
- decltype(std::declval<Collection>().data())>::type>
-Range<T*> range(Collection&& v) {
+template <
+ class Collection,
+ class T = typename std::remove_pointer<
+ decltype(std::declval<Collection>().data())>::type>
+constexpr Range<T*> range(Collection&& v) {
return Range<T*>(v.data(), v.data() + v.size());
}
template <class T, size_t n>
-Range<T*> range(T (&array)[n]) {
+constexpr Range<T*> range(T (&array)[n]) {
return Range<T*>(array, array + n);
}
EXPECT_THROW(a.subpiece(6), std::out_of_range);
}
-constexpr const char* helloArray = "hello";
-
TEST(StringPiece, Constexpr) {
+ constexpr const char* helloArray = "hello";
+
constexpr StringPiece hello1("hello");
EXPECT_EQ("hello", hello1);
static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
testRangeFunc(x, 4);
}
+TEST(RangeFunc, ConstexprCArray) {
+ static constexpr const int numArray[4] = {3, 17, 1, 9};
+ constexpr const auto numArrayRange = range(numArray);
+ EXPECT_EQ(17, numArrayRange[1]);
+ constexpr const auto numArrayRangeSize = numArrayRange.size();
+ EXPECT_EQ(4, numArrayRangeSize);
+}
+
+TEST(RangeFunc, ConstexprIteratorPair) {
+ static constexpr const int numArray[4] = {3, 17, 1, 9};
+ constexpr const auto numPtr = static_cast<const int*>(numArray);
+ constexpr const auto numIterRange = range(numPtr + 1, numPtr + 3);
+ EXPECT_EQ(1, numIterRange[1]);
+ constexpr const auto numIterRangeSize = numIterRange.size();
+ EXPECT_EQ(2, numIterRangeSize);
+}
+
+TEST(RangeFunc, ConstexprCollection) {
+ class IntCollection {
+ public:
+ constexpr IntCollection(const int* d, size_t s) : data_(d), size_(s) {}
+ constexpr const int* data() const {
+ return data_;
+ }
+ constexpr size_t size() const {
+ return size_;
+ }
+
+ private:
+ const int* data_;
+ size_t size_;
+ };
+ static constexpr const int numArray[4] = {3, 17, 1, 9};
+ constexpr const auto numPtr = static_cast<const int*>(numArray);
+ constexpr const auto numColl = IntCollection(numPtr + 1, 2);
+ constexpr const auto numCollRange = range(numColl);
+ EXPECT_EQ(1, numCollRange[1]);
+ constexpr const auto numCollRangeSize = numCollRange.size();
+ EXPECT_EQ(2, numCollRangeSize);
+}
+
std::string get_rand_str(size_t size,
std::uniform_int_distribution<>& dist,
std::mt19937& gen) {