views:

167

answers:

2

Hello, I am trying to use elements of meta programming, but hit the wall with the first trial. I would like to have a comparator structure which can be used as following:

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

Where:

  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; 
    }

    };

What I am missing?

kind regards Arman.

Sorry I did not copy the full error message. Full error:

>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 ==========
A: 

At a guess, strComaparator is defined as something like:

template <int X>
// rest of definition here

When you use such a template, the thing in the angle brackets must always be a compile time constant. But you use the comparator:

strComparator<Tag>()

where Tag is a variable - you can't do that.

anon
but in the boost example we are using following and it works: print_out_by<IDf>(L1.data);where:template<typename Tag,typename MultiIndexContainer>void print_out_by( const MultiIndexContainer typedef typename MultiIndexContainer::value_type value_type; /* dump the elements of the index to cout */std::copy(i.begin(),i.end(),std::ostream_iterator<value_type>(std::cout));}
Arman
@Arman Sorry, that doesn't help. Please confirm the definition of strComparator.
anon
+2  A: 

I suspect that instead of

template<int N> struct strComparator;

you want

template<typename T> struct strComparator;

template <> struct strComparator<Id>{...}; //etc
visitor
Perfect!!!thank you.
Arman