Restrict the 1-argument ArrayView constructor to types with .size() and .data()

Before this change, the argument still needed those methods, but not
having them resulted in a compilation error. Now, it results in this
constructor being removed from the overload set.

This currently makes no difference, but I'm about to publish a CL that
breaks without this.

Review-Url: https://codereview.webrtc.org/2312473002
Cr-Commit-Position: refs/heads/master@{#14068}
This commit is contained in:
kwiberg
2016-09-05 04:20:54 -07:00
committed by Commit bot
parent 59af8b7714
commit bd4317263a
2 changed files with 67 additions and 1 deletions

View File

@ -11,10 +11,37 @@
#ifndef WEBRTC_BASE_ARRAY_VIEW_H_
#define WEBRTC_BASE_ARRAY_VIEW_H_
#include <type_traits>
#include "webrtc/base/checks.h"
namespace rtc {
namespace internal {
// (Internal; please don't use outside this file.) Determines if the given
// class has zero-argument .data() and .size() methods whose return values are
// convertible to T* and size_t, respectively.
template <typename DS, typename T>
class HasDataAndSize {
private:
template <
typename C,
typename std::enable_if<
std::is_convertible<decltype(std::declval<C>().data()), T*>::value &&
std::is_convertible<decltype(std::declval<C>().size()),
size_t>::value>::type* = nullptr>
static int Test(int);
template <typename>
static char Test(...);
public:
static constexpr bool value = std::is_same<decltype(Test<DS>(0)), int>::value;
};
} // namespace internal
// Many functions read from or write to arrays. The obvious way to do this is
// to use two arguments, a pointer to the first element and an element count:
//
@ -95,7 +122,9 @@ class ArrayView final {
// or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and
// rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as
// std::vector).
template <typename U>
template <typename U,
typename std::enable_if<
internal::HasDataAndSize<U, T>::value>::type* = nullptr>
ArrayView(U& u) : ArrayView(u.data(), u.size()) {}
// Indexing, size, and iteration. These allow mutation even if the ArrayView

View File

@ -20,8 +20,45 @@
namespace rtc {
namespace {
namespace test_has_data_and_size {
template <typename C, typename T>
using DS = internal::HasDataAndSize<C, T>;
template <typename DR, typename SR>
struct Test1 {
DR data();
SR size();
};
static_assert(DS<Test1<int*, int>, int>::value, "");
static_assert(DS<Test1<int*, int>, const int>::value, "");
static_assert(DS<Test1<const int*, int>, const int>::value, "");
static_assert(!DS<Test1<const int*, int>, int>::value, ""); // Wrong const.
static_assert(!DS<Test1<char*, size_t>, int>::value, ""); // Wrong ptr type.
struct Test2 {
int* data;
size_t size;
};
static_assert(!DS<Test2, int>::value, ""); // Because they aren't methods.
struct Test3 {
int* data();
};
static_assert(!DS<Test3, int>::value, ""); // Because .size() is missing.
class Test4 {
int* data();
size_t size();
};
static_assert(!DS<Test4, int>::value, ""); // Because methods are private.
} // namespace test_has_data_and_size
template <typename T>
void Call(ArrayView<T>) {}
} // namespace
TEST(ArrayViewTest, TestConstructFromPtrAndArray) {