c++ - Disallow function template instantiation with iterator parameter -
i have function template takes templated parameter:
template <class r> reft<r> make_ref(r& res) { return reft<r>(&res); }
i either want prevent r
being kind of iterator, or, if easier, want have overload compiler prefer use iterators calls make_ref again iterator dereferenced.
best approach combining two, compiler prefers using iterator specific overload, , refuses use non-specific version.
i consumers of code able call make_ref(something)
without having think whether something
iterator or not - need different if is, , if that's not possible, give useful error message consumer.
first traits (you may have tweak requirements):
template <typename t> auto is_iterator_impl(t* it) -> decltype(**it, ++(*it), (*it) == (*it), std::true_type()); template <typename t> auto is_iterator_impl(...) -> std::false_type; template <typename t> using is_an_iterator = decltype(is_iterator_impl<t>(0));
note: using std::iterator_traits<it>
may alternative.
with sfinae, may do
template <class r> std::enable_if_t<!is_an_iterator<r>::value, reft<r>> make_ref(r& res) { return reft<r>(&res); } template <class r> std::enable_if_t<is_an_iterator<r>::value && !std::is_pointer<r>::value, reft<r>> // may want change return type make_ref(r& res) { // implementation iterator } template <class r> std::enable_if_t<std::is_pointer<r>::value, reft<r>> // may want change return type make_ref(r& res) { // implementation iterator }
note: want manage pointer differently, use std::is_pointer
in addition custom is_an_iterator
.
note: conditions should not have overlap, else have conflict.
Comments
Post a Comment