Re: [eigen] binder2nd deprecated |
[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]
On 4/30/2015 19:07, Gabriel wrote:
Hello all :-)Hi! I think I may have an idea for a replacement! :-) To start with: This warning is due to binders being deprecated in C++11: https://thenewcpp.wordpress.com/2012/04/25/deprecated-binders-and-adaptors/ http://en.cppreference.com/w/cpp/utility/functional/binder12 You should not see this warning unless you're compiling with C++11 (or latter) standard. Consequently, I'm assuming you're compiling with C++11 support. Now, the question boils down to what's the C++11 replacement; there are some useful comments in the following: http://stackoverflow.com/questions/19772691/what-replaces-binder2nd-in-c11 This gave me an idea: http://melpon.org/wandbox/permlink/hQsgpVJeTTYFlm7j Note how type `std::binder2nd<std::less<int>>` can be initially replaced with `decltype(std::bind(std::less<int>{}, std::placeholders::_1, 0))`. Now, to generalize it, one could replace `0` with `T{}` or `declval<T>()` -- where `T` is the deduced correct type (here: `int`). In order to actually find out what `T` should be, one needs to automatically infer the callable's argument type -- e.g., akin to something like this: http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html So, either `decltype(std::bind(std::less<int>{}, std::placeholders::_1, std::declval<int>()))`. or perhaps `decltype(std::bind(std::less<int>(), std::placeholders::_1, int{}))`. Depending on whether we want to avoid going through the constructors: http://en.cppreference.com/w/cpp/utility/declval Now: As soon as we know the argument-type of `FUNCTOR<Scalar>` -- assume it's `T` -- I think we can use either `decltype(std::bind(FUNCTOR<Scalar>{}, std::placeholders::_1, std::declval<T>()))`. or perhaps `decltype(std::bind(FUNCTOR<Scalar>, std::placeholders::_1, T{}))`. Again, if we don't know what `T` is, I presume we can deduce it, using a solution similar to that of Boost's function traits? It's still somewhat long-winded, so I'd suggest a simple wrapper, e.g., using alias templates: http://en.cppreference.com/w/cpp/language/type_alias // generally, binders are somewhat readability & maintainability unfriendly, so it's only expected that some of this unfriendliness carries over to the emulators... Here's a one liner wrapper: template <typename T> using binder_2nd_less = decltype(std::bind(std::less<T>(), std::placeholders::_1, T{})); For instance, this allows to replace `std::binder2nd<std::less<int>>` with `binder_2nd_less<int>` in the code. Finally, conditional compilation -- depending on whether C++11 is enabled -- can choose whether to use the C++11-compliant type definition (or whether to fall-back to the `std::binder2nd`). Thoughts? Best, Matt
|
Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |