“Overload” function template based on function object operator() signature in C++98

后端 未结 1 1781
梦如初夏
梦如初夏 2020-12-21 15:35

I want to make a template function that takes a function and a vector and uses the function to map that vector to another vector that will be returned by the function templa

相关标签:
1条回答
  • 2020-12-21 15:50

    Here's my approach:

    template <std::size_t, typename T = void> struct ignore_value {typedef T type;};
    
    template <typename T>
    T& declval();
    
    template<typename F, typename T>
    typename ignore_value<sizeof(declval<F>()(declval<T const>())),
           std::vector<T> >::type map_vec(F fnc, const std::vector<T>& source);
    
    template<typename F, typename T>
    typename ignore_value<sizeof(declval<F>()
                             (declval<T const>(), declval<const std::vector<T> >())),
           std::vector<T> >::type map_vec(F fnc, const std::vector<T>& source);
    

    It works with the same Demo that 0x499602D2 used, with both GCC and Clang in C++98 mode.

    0 讨论(0)
提交回复
热议问题