0

The KDE/PIM Zanshin project uses std::mem_fn in a number of locations throughout its code, and it turns out that at least 1 version of Apple's clang (the one shipped with the latest Xcode available for OS X 10.9) generates object code that fails to link for a number of the files involved.

It turns out to be possible to circumvent the issue by using boost::mem_fn instead of std::mem_fn. The project's main author is not inclined to increase the boost dependency on all platforms, so I proposed a patch in which a conditional macro is used that expands to boost::mem_fn when required.

The request is now to create a template function that lives in one of zanshin's own namespaces (Utils::mem_fn(f)) and that returns either std::mem_fn(f) or boost::mem_fn(f). And that's the part that's either way above my current paygrade ... or that is simply not feasible independent of the fact that I hardly even understand the purpose of the mem_fn function.

So the question is: is there an easy, compact way to wrap std::mem_fn, ideally with a single template function?

The main hurdle appears to be the return type, but since all uses in zanshin's code appear to return what boils down to a function pointer, I tried using a void* return type. I expected that to fail, and indeed it did.

1
  • To clarify: "boost dependency" was shorthand for "having to install boost in order to build the project", not intended to suggest a runtime dependency. I think the reason "he" prefers to add a build dependency on boost only on OS X is that we're most likely dealing with a compiler error here. So the problem ought to solve itself; not using boost::mem_fn everywhere means the code will continue to work with std::mem_fn too.
    – RJVB
    Commented Aug 27, 2015 at 19:08

1 Answer 1

1

"The project's main author is not inclined to increase the boost dependency on all platforms"

So instead he burdens the project with inconsistent dependencies? Sounds crooked.

Also, it's not a dependency in the platform-specific sense at all, since you can simply include the relevant headers inside the codebase (see also BCP) and there is no relevant runtime dependency in the first place.

That said, the simpler option would be to have a wrapper that wraps std::mem_fn and at the same exercises (the address of) the referenced member. That way, the linkage problem should actually disappear.

The simplest thing would be (c++14):

template <typename PTMorPTMF>
    auto my_mem_fn(PTMorPTMF const& ptm) { 
        return std::mem_fn(ptm);
    }

If you're stuck with c++11:

template <typename PTMorPTMF>
    auto my_mem_fn(PTMorPTMF const& ptm) -> decltype(std::mem_fn(ptm)) { 
        return std::mem_fn(ptm);
   }

Simply #ifdef the implementation should you end up implementing it with boost for one platform.

4
  • That's interesting: the link error actually starts with "decltype" following by a whole lot of crud that somewhere makes it clear that std::mem_fn is the actual missing function. I'll try your suggestion; seems that either it'll allow me to get rid of boost::mem_fn altogether, or else I should be able to use a conditional form that wraps either std::mem_fn or boost::mem_fn .
    – RJVB
    Commented Aug 27, 2015 at 19:01
  • I agree that you can do your own thing based on std::bind instead of boost::mem_fn. Your linker error is meaningless without any accompanying code. I have no clue what you're trying point out with the linker error.
    – sehe
    Commented Aug 27, 2015 at 21:01
  • the link error was just to illustrate; I doubt it's useful to anyone except for figuring out exactly what kind of compiler bug causes it. Finding a solution that doesn't involve boost::mem_fn is an exercise I'll leave for another time. Or life :)
    – RJVB
    Commented Aug 27, 2015 at 21:13
  • Ok! Thanks for clarifying
    – sehe
    Commented Aug 27, 2015 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.