简体   繁体   中英

c++ Setting equal_to as a default template argument for template class

How exactly are you supposed to set a generic class as a default template argument? I've managed to get non-generic classes to compile, but not generic ones.

using namespace std;
template <typename T, typename Cmp = std::equal_to<T> >
class CSearch
{
   public:
    CSearch(){
     MComp = equal_to<T>();
    }
    CSearch(Cmp compare){
     MComp = compare;

    }
    //more functions
   private:
    Cmp MComp;
    //more 
};

T is any data type with an iterator

Cmp is supposed to be a Binary Predicate function/functor/lambda function. Cmp is called like this:

MComp(*it2,*it3);

Now with this main function, the code works. Note that CharComparator isn't really that different from equal_to except for being non-generic.

class CharComparator
{
  public:
               CharComparator ( bool caseSensitive = true )
      : m_CaseSensitive ( caseSensitive )
    {
    }
    bool       operator () ( const char & a, const char & b ) const
    {
      return m_CaseSensitive ? a == b : toupper (a) == toupper (b);
    }
  private:
    bool       m_CaseSensitive;
};
int main(){
CSearch <string, CharComparator> test3 ( CharComparator(false) );
//functions,calling Cmp
}

But this one doesn't compile when Cmp is called:

int main(){
CSearch <string> test1;
//functions,calling Cmp
}

Here's the compiler error.

test.cpp: In instantiation of 'bool CSearch<T, Cmp>::SearchFragment(const T&, const T&) const [with T = std::__cxx11::basic_string<char>; Cmp = std::equal_to<std::__cxx11::basic_string<char> >]':
test.cpp:88:6:   required from 'std::set<int> CSearch<T, Cmp>::Search(const T&) const [with T = std::__cxx11::basic_string<char>; Cmp = std::equal_to<std::__cxx11::basic_string<char> >]'
test.cpp:130:3:   required from here
test.cpp:60:3: error: no match for call to '(const std::equal_to<std::__cxx11::basic_string<char> >) (const char&, const char&)'
   60 |   if(MComp(*it2,*it3)){
      |   ^~
In file included from /usr/include/c++/9.3/string:48,
                 from /usr/include/c++/9.3/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3/bits/ios_base.h:41,
                 from /usr/include/c++/9.3/ios:42,
                 from /usr/include/c++/9.3/ostream:38,
                 from /usr/include/c++/9.3/iostream:39,
                 from test.cpp:5:
/usr/include/c++/9.3/bits/stl_function.h:355:7: note: candidate: 'constexpr bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::__cxx11::basic_string<char>]'
  355 |       operator()(const _Tp& __x, const _Tp& __y) const
      |       ^~~~~~~~
/usr/include/c++/9.3/bits/stl_function.h:355:29: note:   no known conversion for argument 1 from 'const char' to 'const std::__cxx11::basic_string<char>&'
  355 |       operator()(const _Tp& __x, const _Tp& __y) const
      |                  ~~~~~~~~~~~^~~

This line of the error says what is wrong:

error: no match for call to '(const std::equal_to<std::__cxx11::basic_string<char> >) (const char&, const char&)'

You try to call an instance of std::equal_to<std::string> to compare two character, but std::equal_to<std::string> compares strings.

You want std::equal_to< typename T::value_type> not std::equal_to<T> because you are comparing characters not strings (or in general elements when T is a container type).

Also you should not assign MComp = equal_to<T>(); in the default constructor. If Cmp is something else than equal_to<T> the default constructor wont work. You should instead initialize MComp with a default instance of type Cmp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM