What is the reason for `std::result_of` deprecated in C++17?

后端 未结 2 1176
时光说笑
时光说笑 2020-12-15 18:47

I saw std::result_of is being deprecated in C++17.

  • What is the reason for std::result_of deprecated in C++17?
  • Also I would
2条回答
  •  感情败类
    2020-12-15 19:26

    @haelix:

    I'm totaly with you concerning the lack of example on the cppreference page. Here's my take:

    auto add_auto_fn(int a, int b) {
        return a + b;
    }
    
    template
    auto add_auto_template_fn(U a, V b) {
        return a + b;
    }
    
    int fortytwo(int a, int b) { return a + 42; }
    
    struct my_functor{
        auto operator() (int a) { return a + 42; }
    };
    
    void test_invoke_result()
    {
        {
            // For functions and auto function: use < decltype(&f), Args... >
            using T = std::invoke_result< decltype(&fortytwo), int, int>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For templated auto functions: use < decltype(&f), Args... >
            using T = std::invoke_result< decltype(&add_auto_template_fn), int, double>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For simple lambdas: use < decltype(lambda), Args... >
            auto simple_lambda = [](int a) {  return a + 42; };
            using T = std::invoke_result< decltype(simple_lambda), int>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For generic lambdas: use < decltype(lambda), Args... >
            auto generic_lambda = [](auto a) {  return a + 42; };
            using T = std::invoke_result< decltype(generic_lambda), double>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For functors: use < functor, Args... >
            using T = std::invoke_result< my_functor, int>::type;
            static_assert(std::is_same::value, "");
        }
    
    }
    
    void test_result_of()
    {
        {
            // For functions and auto function: use < decltype(&f)(Args...) >
            using T = std::result_of< decltype(&fortytwo)(int, int)>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For templated auto functions: use < decltype(&f)(Args...) >
            using T = std::result_of< decltype(&add_auto_template_fn)(int, double)>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For simple lambdas: use < decltype(lambda)(Args...) >
            auto simple_lambda = [](int a) {  return a + 42; };
            using T = std::result_of< decltype(simple_lambda)(int)>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For generic lambdas: use < decltype(lambda)(Args...) >
            auto generic_lambda = [](auto a) {  return a + 42; };
            using T = std::result_of< decltype(generic_lambda)(double)>::type;
            static_assert(std::is_same::value, "");
        }
        {
            // For functors: use < functor(Args...) >
            using T = std::result_of< my_functor(int)>::type;
            static_assert(std::is_same::value, "");
        }
    
    }
    

提交回复
热议问题