Boost bind and'result_type':不是成员,c ++ 03友好

问题描述 投票:0回答:1

Visual Studio 2019的最新16.6更新删除了std::plus::result_typestd::minus::result_type和相关的typedef。 (它们在C ++ 17中已弃用,在C ++ 20中已删除。)代码的简化版本如下所示:

template <typename FF>
struct function_wrapper {
    function_wrapper(FF func, const std::string& name) : func_(func), name_(name) { }
    int operator()(int i1, int i2) const { return func_(i1, i2); }
    // ... other stuff ...
    FF func_;
    std::string name_;
};

template <typename FF>
int use_function(const function_wrapper<FF>& func, const std::pair<int, int>& args) {
    return func(args.first, args.second);
}

funcWrapper<boost::function<int(int, int)>> plus_func(std::plus<int>(), "plus");
std::cout << use_function(plus_func, std::make_pair<int, int>(1, 2)) << std::endl;

更新后,它将不再编译,并产生错误:

boost\1.72.0\boost\bind\bind.hpp(75,25): error C2039: 'result_type': is not a member of 'std::plus<int>'

我无法将result_type typedef添加回std::plus,因此我需要另一种方法来解决此问题。事实是,生成的代码也需要在C ++ 03下编译,因此lambda和> = C ++ 11构造不是可行的选择。我可以重新实现std::plus并自己添加现在已删除的typedef以使Boost开心,但是还有更好的方法吗?

c++ visual-studio-2019 c++03 c++20 boost-bind
1个回答
0
投票

用:包裹旧函子>]

template <typename F>
struct functor;

template <template <typename> class F, typename T>
struct functor<F<T>> : F<T>
{
    typedef T result_type;
    typedef T first_argument_type;
    typedef T second_argument_type;
};

然后:

function_wrapper<boost::function<int(int, int)>>
     plus_func(functor<std::plus<int>>(), "plus");
//             ~~~~~~~^             ~^~
© www.soinside.com 2019 - 2024. All rights reserved.