template <typename InputIterator>
MyFun(const InputIterator begin, const InputIterator end)
{
// I want to static_assert that decltype(*begin) == SomeType
}
How can I do this? I'm thinking static_assert(std::is_same<*InputIterator,SomeType>)
but that of course does not work...
std::iterator_traits:
static_assert(is_same<typename std::iterator_traits<InputIterator>::value_type,
SomeType>::value, "");
An alternative using decltype
, with a note in passing that it often produces a reference (but may not!).
// If you want to assert that the dereferenced item is indeed a reference
static_assert(std::is_same<decltype(*begin), SomeType&>::value, "");
// If you are only interested in the "bare" type
// (equivalent to Jesse's providing iterator_traits was properly specialized)
static_assert(std::is_same<
typename std::remove_reference<decltype(*begin)>::type,
SomeType
>::value, "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With