如何获取模板成员函数的结果类型?
以下最小示例说明了该问题。
#include <type_traits>
template <typename U>
struct A {
};
struct B {
template <typename F = int>
A<F> f() { return A<F>{}; }
using default_return_type = std::invoke_result_t<decltype(f)>;
};
int main()
{
B::default_return_type x{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看到它住在Coliru。
代码不编译,报错:
main.cpp:11:63: 错误:decltype 无法解析重载函数的地址
11 | 使用 default_return_type = std::invoke_result_t;
B::f在模板参数F设置为默认值的情况下获取类型的正确语法是什么?
如何为成员函数正确调用 invoke_result?或者专门用于操作员成员函数。我试过std::invoke_result<T::operator[], size_type>没有成功。在这种情况下,正确的语法是什么?
如何在 C++ 中使用 std::invoke_result_t 获取类成员函数的返回类型?
#include <type_traits>
#include <vector>
template <class T>
struct C
{
auto Get(void) const { return std::vector<T>{1,2,3}; }
};
int main(void)
{
// what should one put below to make x to have type std::vector<int> ?
std::invoke_result_t<C<int>::Get, void> x;
// ^^^^^^^^^^^^^^^^^
return 0;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!