是否可以在没有成员函数“const”限定符的情况下获取成员函数的类型(返回类型和签名)?
到目前为止,我尝试使用 decltype(T) 和 std::remove_const/std::decay。
示例:
void Func(std::string, int) const -> void(std::string, int)
编辑以澄清:
我有:
void Func(std::string, int) const
我想要什么:
void(std::string, int)
最佳答案
这是使用 CTAD 执行此操作的方法:
struct C
{
void Func(std::string, int) const;
void Bar(int);
};
template<typename T, typename Ret, typename... Args>
struct GetType
{
using type = Ret(Args...);
GetType(Ret (T::*)(Args...) const)
{
}
GetType(Ret (T::*)(Args...) )
{
}
};
int main()
{
using ret1 = decltype(GetType(&C::Func))::type; // void(std::string, int) as expected
using ret2 = decltype(GetType(&C::Bar))::type; //void(int) as expected
}
关于C++ 获取成员函数的类型(返回类型和签名),不带成员函数的 'const' 限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76127695/