0

(This is in part a follow up to this question of mine.)

As I've written in this self-answer, I've discovered that Boost offers a macro to wrap a template function in a function object so it can be passed to higher-order functions:

#include <boost/hof.hpp>
#include <cassert>
#include <algorithm>

// Declare the class `max_f`
BOOST_HOF_LIFT_CLASS(max_f, std::max);

int main() {
    auto my_max = BOOST_HOF_LIFT(std::max);
    assert(my_max(3, 4) == std::max(3, 4));
    assert(max_f()(3, 4) == std::max(3, 4));
}

The point is that things don't quite work well with MSVC, and the problem hits me especially when some of the template parameters cannot be deduced and corresponding arguments must be provided explicitly, as is the case for std::get, just to make an example.

The following works with GCC, but errors with MSVC

template<int N>
auto constexpr getNth_a = BOOST_HOF_LIFT(std::get<N>);
// usage
std::tuple<int,int> t{1,2};
getNth_a<0>(t); // errors with MSVC

So I tried BOOST_HOF_LIFT_CLASS instead, but I can't get it working when templated, not even in GCC:

template<int N>
BOOST_HOF_LIFT_CLASS(getNth_b, std::get<N>);
// attempted usage
std::tuple<int,int> t{1,2};
getNth_b<0>(t); // doens't even work with GCC

Here's a demo.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • Your `getNth_b` problem is of a different nature. You use it wrong (and the example should have been here, not behind a link). `getNth_b<0>` is a type, so `getNth_b<0>(t);` is a variable (re-)declaration (most vexing parse). To create a functor instance and immediately call it we'd do `getNth_b<0> {} (t);` – StoryTeller - Unslander Monica Jul 13 '21 at 19:47
  • @StoryTeller-UnslanderMonica, I've included the example usage in the question. – Enlico Jul 14 '21 at 04:44
  • @StoryTeller-UnslanderMonica, I have no idea why I deleted this. However, feel free to post the comment as answer, and I'll accept it. – Enlico Jul 22 '21 at 07:33
  • For some reason, I see my question copied [here](https://windowsquestions.com/2021/07/13/how-do-i-use-boost_hof_lift-and-boost_hof_lift_class-with-msvc/). – Enlico Jul 22 '21 at 07:51

0 Answers0