比较范围构造函数中的迭代器 value_type
Comparing iterator value_type in range constructor
我正在为自定义容器编写范围构造函数:
MyContainer(const InputIterator& first,
const InputIterator& last,
const allocator_type& alloc = allocator_type())
并想检查 InputIterator::value_type
是否与容器的 value_type
兼容。我最初尝试过:
static_assert(!(std::is_convertible<InputIterator::value_type, value_type>::value ||
std::is_same<InputIterator::value_type, value_type>::value), "error");
并达到了:
using InputIteratorType = typename std::decay<InputIterator>::type;
using InputValueType = typename std::decay<InputIteratorType::value_type>::type;
static_assert(!(std::is_convertible<InputValueType, value_type>::value ||
std::is_same<InputValueType, value_type>::value), "error");
但它总是断言,即使我使用 MyContainer::iterator
作为输入迭代器也是如此。
如何检查 InputIterator
是否兼容?
我猜你可能想要 std::is_constructible
:
static_assert(std::is_constructible<
value_type,
decltype(*first)
>::value, "error");
或者 std::is_assignable
,具体取决于您是从输入迭代器构造还是分配。
我正在为自定义容器编写范围构造函数:
MyContainer(const InputIterator& first,
const InputIterator& last,
const allocator_type& alloc = allocator_type())
并想检查 InputIterator::value_type
是否与容器的 value_type
兼容。我最初尝试过:
static_assert(!(std::is_convertible<InputIterator::value_type, value_type>::value ||
std::is_same<InputIterator::value_type, value_type>::value), "error");
并达到了:
using InputIteratorType = typename std::decay<InputIterator>::type;
using InputValueType = typename std::decay<InputIteratorType::value_type>::type;
static_assert(!(std::is_convertible<InputValueType, value_type>::value ||
std::is_same<InputValueType, value_type>::value), "error");
但它总是断言,即使我使用 MyContainer::iterator
作为输入迭代器也是如此。
如何检查 InputIterator
是否兼容?
我猜你可能想要 std::is_constructible
:
static_assert(std::is_constructible<
value_type,
decltype(*first)
>::value, "error");
或者 std::is_assignable
,具体取决于您是从输入迭代器构造还是分配。