代码之家  ›  专栏  ›  技术社区  ›  Geoff

使用mpl实现现有元函数的部分专门化

  •  0
  • Geoff  · 技术社区  · 16 年前

    我想对boost库中的range\u mutable\u iterator和range\u const\u iterator进行部分专门化,但只针对我不想明确提及的特定类型,而只允许在enable\u if测试条件通过时选择部分专门化。

    我当前正在使用MSVC 2008,收到以下错误:
    ArrayType : template parameter not used or deducible in partial specialization

    range_mutable_iterator<
      typename enable_if<
        mpl::and_<
          mpl::has_key<some_type_map, typename remove_const<T>::type>,
          mpl::not_<is_const<T> >
        >,
        ArrayType
      >::type
    >
    

    使用STLFilt时,请注意对T而不是ArrayType的奇怪引用,我猜STLFilt是说它无法找出T==ArrayType。。?我现在的情况是:

    namespace boost {
    template<class ArrayType>
    struct range_mutable_iterator<
      typename enable_if<
        mpl::and_<
          mpl::has_key<some_type_map, typename remove_const<ArrayType>::type>,
          mpl::not_<is_const<ArrayType> >
        >,/*and_*/
        ArrayType
      >::type/*enable_if*/
    >
    {
      typedef MyArrayIterator<
        typename mpl::at<some_other_type_map,
          typename mpl::at<yet_another_type_map,ArrayType>::type
        >::type/*at*/
      >/*MyArrayIterator*/ type;
    };
    }
    

    ThirdPartyArrayClass blah;
    MyArrayAdapter<ThirdPartyArrayClass>::iterator iter = boost::begin(blah);
    

    编辑:在尝试了一种不同的方法之后,我从这个答案中删掉了它,我开始接受在这个例子中部分专门化是不可能的,所以我使用了一种不同的方法,包括完全专门化和大量使用Boost.Preprocessor。

    1 回复  |  直到 16 年前
        1
  •  0
  •   hkaiser    16 年前

    为了部分专门化模板,编译器必须将实际的模板参数与现有的专门化匹配,以选择最佳匹配。如果专门化中的template参数只是用作依赖类型的(template)参数,则这是不可能的。因此,编译器会抱怨,这是正确的。你唯一能做的就是专门研究你的混凝土 ThirdPartyArrayClass

    推荐文章