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
| ~~~~~~~~~~~^~~
Aucun commentaire:
Enregistrer un commentaire