I am not sure about the effect of const modifier and reference in the template argument of std::function. For example, in the following codes, should I use std::function<bool(std::string, std::string)> or std::function<bool(const std::string&, const std::string&)> as the base class ? I tested both in GCC 4.4, however, there was no difference. Thanks in advance.
#include <iostream>
#include <functional>
#include <string>
//struct FLess : public std::function<bool(std::string, std::string)>
struct FLess : public std::function<bool(const std::string&, const std::string&)>
{
bool operator () (const std::string& s1, const std::string& s2) const
{
return s1 < s2;
}
};
int main(int argc, char* argv[])
{
FLess f;
std::string a = "a";
std::string b = "b";
std::cerr << f(a, b) << std::endl;
return 0;
}
It boils down to compatibility between the function object’s parameter types and those of the callable entity it is referring to, if the function’s parameter types can be converted to the callable entities parameter types or not:
Interestingly, an std::function with void return type is compatible with callable entities with any return type:
So in your case, where you are comparing passing by const reference as opposed to passing by value, I think there is no observable difference.