Jai*_*ime 6 c++ templates decltype member-pointers c++11
我知道这在C++ 03中是不可能的,但我希望有一些新的巫术允许我这样做.见下文:
template <class T>
struct Binder
{
template<typename FT, FT T::*PtrTomember>
void AddMatch();
};
struct TestType
{
int i;
};
int main(int argc, char** argv)
{
Binder<TestType> b;
b.AddMatch<int,&TestType::i>(); //I have to do this now
b.AddMatch<&TestType::i>(); //I'd like to be able to do this (i.e. infer field type)
}
Run Code Online (Sandbox Code Playgroud)
在C++ 11中有没有办法做到这一点?decltype会有帮助吗?
**更新:使用Vlad的例子我认为这样的事情会起作用(警告:我没有编译,因为我正在构建具有decltype支持的编译器)
template <class T>
struct Binder
{
template<typename MP, FT ft = decltype(MP)>
void AddMatch()
{
//static_assert to make sure MP is a member pointer of T
}
};
struct TestType
{
int i;
};
int main()
{
Binder<TestType> b;
b.AddMatch<&TestType::i>();
}
Run Code Online (Sandbox Code Playgroud)
这会有用吗?
| 归档时间: |
|
| 查看次数: |
2862 次 |
| 最近记录: |