Gra*_*ind 9 c++ templates metaprogramming c++11
我正努力做这项工作:
template < typename T, T VALUE >
void f()
{
/* ... */
}
int main()
{
f<10>(); // implicit deduction of [ T = int ] ??
return (0);
}
Run Code Online (Sandbox Code Playgroud)
目的是简化更复杂的模板.
经过多次搜索,我在C++ 0x上找不到任何方法,所以stackoverflow是我最后的选择.
C++ 0x引入decltype(),它完全符合您的要求.
int main()
{
f<decltype(10), 10>(); // will become f<int, 10>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++中的结构/类没有自动模板推导.你可以做的就是这样(警告,未经测试!):
#define F(value) f<decltype(value), value>
template < typename T, T VALUE >
void f()
{
/* ... */
}
int main()
{
F(10)();
return (0);
}
Run Code Online (Sandbox Code Playgroud)
它不像模板专用代码那样干净,但它清楚它的功能,并且可以避免重复自己的负担.如果需要它在非C++ 0x编译器上工作,可以使用Boost.Typeof而不是decltype.
| 归档时间: |
|
| 查看次数: |
888 次 |
| 最近记录: |