If I have code such as:
std::wstring s(L"...");
bool allCharsEqual =
std::find_if(s.begin(),
s.end(),
std::bind1st(std::not_equal_to<std::wstring::value_type>(),
mystring[0])) // ^^^^^^^^^^^^^^^^^^^^^^^^
== s.end();
I would like to have a generic expression in the marked location which would also work if I changed the variable type to std::string
. Is this possible?
I realize I could put this in an extra function and template it with the string type, but is there another way?
You can use decltype
in C++11:
std::not_equal_to<decltype(s)::value_type>()
Or in C++14 you can use transparent comparators and just skip the template argument entirely:
std::not_equal_to<>()
If you're stuck with C++03, you could make a typedef
so that you only have a single place you need to update to change types. This is good practice in general if you can't use auto
and have types which you forsee changing.
typedef std::wstring str_t