1

我正在尝试使用元编程元素,但在第一次尝试时碰壁了。我想要一个比较器结构,可以使用如下:

intersect_by<ID>(L1.data, L2.data, "By ID: ");
intersect_by<IDf>(L1.data, L2.data, "By IDf: ");

在哪里:

  struct ID{};// Tag used for original IDs
    struct IDf{};// Tag used for the file position  
    //following Boost.MultiIndex examples  
    template<typename Tag,typename MultiIndexContainer>
    void intersect_by(
    const MultiIndexContainer& L1,const MultiIndexContainer&    L2,std::string msg,
      Tag* =0 /* fixes a MSVC++ 6.0 bug with implicit template function parms */
    )
    {
     /* obtain a reference to the index tagged by Tag */

     const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& L1_ID_index=
     get<Tag>(L1);
     const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& L2_ID_index=
     get<Tag>(L2);

      std::set_intersection(
      L1_ID_index.begin(), 
      L1_ID_index.end(), 
      L2_ID_index.begin(), 
      L2_ID_index.end(),
      std::inserter(s, s.begin()), strComparator<Tag>() // Here I get the C2975 error
      );
      }

    template<int N> struct strComparator;
    template<> 
    struct strComparator<0>{
    bool operator () (const particleID& id1, const particleID& id2) const
    {
     return id1.ID<id2.ID;
    }
    };
    template<> 
    struct strComparator<1>{ 
    bool operator () (const particleID& id1, const particleID& id2) const
    {
     return id1.IDf<id2.IDf; 
    }

    };

我错过了什么?

亲切的问候阿尔曼。

抱歉,我没有复制完整的错误消息。完整错误:

>Compiling...
1>trace_ids.cpp
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(133) : error C2975: 'strComparator' : invalid template argument for 'N', expected compile-time constant expression
1>        c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(47) : see declaration of 'strComparator'
1>        c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(144) : see reference to function template instantiation 'void intersect_by<ID,particlesID_set>(const MultiIndexContainer &,const MultiIndexContainer &,std::string,Tag *)' being compiled
1>        with
1>        [
1>            MultiIndexContainer=particlesID_set,
1>            Tag=ID
1>        ]
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(133) : error C2955: 'strComparator' : use of class template requires template argument list
1>        c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(47) : see declaration of 'strComparator'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2784: '_OutElem *std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutElem (&)[_Size],_Pr)' : could not deduce template argument for '_OutElem (&)[_Size]' from 'std::insert_iterator<_Container>'
1>        with
1>        [
1>            _Container=particlesID_set
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(4064) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2780: '::std::_Enable_if<!std::_Is_checked_iterator<_OutIt>::_Result,_OutIt>::_Result std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' : expects 5 arguments - 6 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(4004) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2780: '_OutElem *std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutElem (&)[_Size])' : expects 5 arguments - 6 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(3993) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2780: '::std::_Enable_if<std::_Is_checked_iterator<_OutIt>::_Result,_OutIt>::_Result std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' : expects 5 arguments - 6 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(3983) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(133) : error C2975: 'strComparator' : invalid template argument for 'N', expected compile-time constant expression
1>        c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(47) : see declaration of 'strComparator'
1>        c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(146) : see reference to function template instantiation 'void intersect_by<IDf,particlesID_set>(const MultiIndexContainer &,const MultiIndexContainer &,std::string,Tag *)' being compiled
1>        with
1>        [
1>            MultiIndexContainer=particlesID_set,
1>            Tag=IDf
1>        ]
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(133) : error C2955: 'strComparator' : use of class template requires template argument list
1>        c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(47) : see declaration of 'strComparator'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2784: '_OutElem *std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutElem (&)[_Size],_Pr)' : could not deduce template argument for '_OutElem (&)[_Size]' from 'std::insert_iterator<_Container>'
1>        with
1>        [
1>            _Container=particlesID_set
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(4064) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2780: '::std::_Enable_if<!std::_Is_checked_iterator<_OutIt>::_Result,_OutIt>::_Result std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' : expects 5 arguments - 6 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(4004) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2780: '_OutElem *std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutElem (&)[_Size])' : expects 5 arguments - 6 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(3993) : see declaration of 'std::set_intersection'
1>c:\documents and settings\arm2arm\mes documents\visual studio 2008\projects\mstgraph\trace_ids\trace_ids.cpp(134) : error C2780: '::std::_Enable_if<std::_Is_checked_iterator<_OutIt>::_Result,_OutIt>::_Result std::set_intersection(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' : expects 5 arguments - 6 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\algorithm(3983) : see declaration of 'std::set_intersection'
1>Build log was saved at "file://c:\Documents and Settings\arm2arm\Mes documents\Visual Studio 2008\Projects\mstgraph\trace_ids\Debug\BuildLog.htm"
1>trace_ids - 12 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

2 回答 2

2

我怀疑不是

template<int N> struct strComparator;

你要

template<typename T> struct strComparator;

template <> struct strComparator<Id>{...}; //etc
于 2010-03-17T09:58:49.043 回答
0

猜测一下,strComaparator 被定义为:

template <int X>
// rest of definition here

当您使用这样的模板时,尖括号中的内容必须始终是编译时间常数。但是您使用比较器:

strComparator<Tag>()

其中 Tag 是一个变量 - 你不能这样做。

于 2010-03-17T09:37:08.430 回答