Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果U是接口的后代,则启用_<;T>;_C++_Templates_Interface_C++14_Template Specialization - Fatal编程技术网

C++ 如果U是接口的后代,则启用_<;T>;

C++ 如果U是接口的后代,则启用_<;T>;,c++,templates,interface,c++14,template-specialization,C++,Templates,Interface,C++14,Template Specialization,最多C++14 在我的示例中,我有一个用于自定义迭代器的begin()。到目前为止,我有这个 template<typename T> inline auto begin(ISomeIterator<T> &it) -> RangeForISomeIterator<ISomeIterator<T>> { return it; } template<typename T> inline auto begin(ISo

最多C++14

在我的示例中,我有一个用于自定义迭代器的
begin()
。到目前为止,我有这个

template<typename T>
inline auto
begin(ISomeIterator<T> &it)
  -> RangeForISomeIterator<ISomeIterator<T>>
{
  return it;
}

template<typename T>
inline auto
begin(ISomeConstIterator<T> &it)
  -> RangeForISomeConstIterator<ISomeConstIterator<T>>
{
  return it;
}
只需声明正确的成员,并在
std::is_const
上发送

名称空间详细信息
{
模板
内联自动开始(IT&&IT,std::false\U类型)
->等距迭代器
{
返回std::转发(it);
}
模板
内联自动开始(IT&&IT,std::true\u类型)
->测距仪
{
返回std::转发(it);
}
}
模板
内联自动开始(IT&&IT)
->decltype(detail::begin(std::forward(it),std::is_const{}))
{
返回细节::begin(std::forward(it),std::is_const{});
}

顺便问一下,为什么你有迭代器(公开)从任何东西降序

我不明白这个问题
begin()
获取一个范围并返回一个迭代器-看起来您正在获取一个迭代器并返回一个范围。也不清楚您想要实现什么。您可能不应该尝试强加一个不同于
std
的迭代思想,可能我继承了一些代码。:-)如果我们忘记了迭代器,那么最初的问题仍然很有趣,
std::forward
lvalue@CalethDropped.Re P..S:我们不能在隐藏实现的API上使用模板。如果我们忘记迭代器,最初的问题仍然很有趣。我喜欢你对我给出的例子的回答。
begin(U &it)
// where U is a descendant of ISomeIterator<T>

begin(U &it)
// where U is a descendant of ISomeConstIterator<T>
namespace detail
{
    template<typename IT>
    inline auto begin(IT&& it, std::false_type)
      -> RangeForISomeIterator<IT>
    {
      return std::forward<IT>(it);
    }

    template<typename IT>
    inline auto begin(IT&& it, std::true_type)
      -> RangeForISomeConstIterator<IT>
    {
      return std::forward<IT>(it);
    }
}

template<typename IT>
inline auto begin(IT&& it)
  -> decltype(detail::begin(std::forward<IT>(it), std::is_const<typename std::iterator_traits<It>::value_type>{}))
{
  return detail::begin(std::forward<IT>(it), std::is_const<typename std::iterator_traits<It>::value_type>{});
}