C++ C++;11指向模板化静态成员函数的指针向量
假设我有一门课是这样的:C++ C++;11指向模板化静态成员函数的指针向量,c++,c++11,C++,C++11,假设我有一门课是这样的: struct MyStruct { template<typename T> static int Func() { // Do something with the template parameter. } }; 然后,创建向量非常简单: std::vector<FuncPtr> FuncVector; std::vector FuncVector; 但是,我希望避免两次声明Func的原型
struct MyStruct
{
template<typename T>
static int Func()
{
// Do something with the template parameter.
}
};
然后,创建向量非常简单:
std::vector<FuncPtr> FuncVector;
std::vector FuncVector;
但是,我希望避免两次声明
Func
的原型。可以这样做吗?您可以执行以下操作,而不是FuncPtr
的typedef
:
using FuncPtr = decltype(&MyStruct::Func<int>);
使用FuncPtr=decltype(&MyStruct::Func);
对于FuncPtr
,您可以执行以下操作,而不是使用typedef
:
using FuncPtr = decltype(&MyStruct::Func<int>);
使用FuncPtr=decltype(&MyStruct::Func);
使用decltype
:
std::vector<decltype(&MyStruct::Func<void>)> FuncVector;
std::vector FuncVector;
使用decltype
:
std::vector<decltype(&MyStruct::Func<void>)> FuncVector;
std::vector FuncVector;
既然你说原型不依赖于参数,那么干脆std::vector
怎么样?当两个声明不一致时,有一个很好的理由避免两次声明Func
的原型。既然你说原型不依赖于参数,简单地std::vector
怎么样?当两个声明不一致时,有一个很好的理由可以避免两次声明Func
的原型。