【问题标题】:What does the return type template(enable_if) prohibit?返回类型模板(enable_if)禁止什么?
【发布时间】:2011-12-28 08:37:39
【问题描述】:

以下内容摘自 vs 2010 map 实现,该成员函数定义在父类 _Tree 中,在文件 xtree 中。

template<class _Valty>
    typename _STD tr1::enable_if<!_STD tr1::is_same<const_iterator,
        typename _STD tr1::remove_reference<_Valty>::type>::value,
            iterator>::type
    insert(const_iterator _Where,
        _Valty&& _Val)
    {   // try to insert node with value _Val using _Where as a hint
    return (_Insert(_Where,
        this->_Buynode(_STD forward<_Valty>(_Val))));
    }

其实这个函数是iterator insert ( iterator position, const value_type& x ),有趣的是它的返回类型!这是什么意思?据我了解,如果 value_type(i.e. _Valty) 与 const_iterator 相同,它将禁用模板实例化。

但是,在这种情况下,由于没有从 const_iteratorvalue_type 的隐式转换,这看起来是多余的。

我相信我有一些我不明白的东西,它是什么?返回类型模板禁止什么?

【问题讨论】:

    标签: c++ visual-studio-2010 visual-c++ c++11


    【解决方案1】:

    还有其他insert 成员接受一系列对象,[iterator, iterator)。

    enable_if 用于通过仅在涉及的类型不同时启用此功能来避免重载解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2021-06-16
      相关资源
      最近更新 更多