You can mimic extension functions in C++, by abusing free-standing operator overloading. Here I've leveraged operator->*
.
#include <functional>
#include <iostream>
#include <string>
// Extension function.
static int functionName(std::string const& self) {
if (self == "something") {
return 1;
}
return 0;
}
// Thunk glue.
static int operator->*(std::string const& s, int(*fn)(std::string const&)) {
return fn(s);
}
int main() {
std::string s = "something";
int i = s->*functionName;
std::cout << i << "\n";
}
But I strongly urge you to wait until some future C++ standard formally adopts unified function calls.
Editorial note: not recommended, as it is not idiomatic C++ and abusing operator overloading is a good way to make you unpopular. And C++ is already a difficult enough language as is.