msc*_*msc 15 c++ templates deprecated result-of c++17
我看到std::result_of在C++中被弃用了17.
std::result_of在C++ 17中弃用的原因是什么?std::result_of和std::invoke_result.@海利克斯:
对于 cppreference 页面上缺少示例的问题,我完全同意您的看法。这是我的看法:
auto add_auto_fn(int a, int b) {
return a + b;
}
template<typename U, typename V>
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<T, int>::value, "");
}
{
// For templated auto functions: use < decltype(&f)<Args...>, Args... >
using T = std::invoke_result< decltype(&add_auto_template_fn<int, double>), int, double>::type;
static_assert(std::is_same<T, double>::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<T, int>::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<T, double>::value, "");
}
{
// For functors: use < functor, Args... >
using T = std::invoke_result< my_functor, int>::type;
static_assert(std::is_same<T, int>::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<T, int>::value, "");
}
{
// For templated auto functions: use < decltype(&f<Args...>)(Args...) >
using T = std::result_of< decltype(&add_auto_template_fn<int, double>)(int, double)>::type;
static_assert(std::is_same<T, double>::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<T, int>::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<T, double>::value, "");
}
{
// For functors: use < functor(Args...) >
using T = std::result_of< my_functor(int)>::type;
static_assert(std::is_same<T, int>::value, "");
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它,你必须从
std::result_of_t<A(B)>
Run Code Online (Sandbox Code Playgroud)
到
std::invoke_result_t<A, B>
Run Code Online (Sandbox Code Playgroud)
或在模板中
std::result_of_t<F(Args...)>
Run Code Online (Sandbox Code Playgroud)
到
std::invoke_result_t<F,Args...>
Run Code Online (Sandbox Code Playgroud)
TC已经提供的明显的联系,但也许最可怕的原因熊重复:result_of参与形成该类型F(Arg1, Arg2, ...)不为这些类型的一个函数返回 F但对于一个功能类型的 F接受的那些类型。(毕竟,返回类型是输入的结果result_of,而不是输入!)
除了与形成函数类型相关的调整之外,两者之间的唯一区别是语法。
| 归档时间: |
|
| 查看次数: |
1473 次 |
| 最近记录: |