我正在用C++编写一些模板代码,如果我能确定类型,我就会使代码更短/更好/更有用this.我不想使用C++ 0x,因为代码是与旧编译器向后兼容的.我也不想使用BOOST.我所拥有的是:
struct MyLoop {
template <class Param>
void Run(int iterations, Context c)
{
MyUtility<MyLoop>::template WrapLoop<Param>(iterations, c);
}
};
Run Code Online (Sandbox Code Playgroud)
这可以用于一些有趣的循环优化.我不喜欢MyLoop的MyUtility模板特殊化.使用C++ 0x,可以使用以下内容:
struct MyLoop {
template <class Param>
void Run(int iterations, Context c)
{
MyUtility<decltype(*this)>::template WrapLoop<Param>(iterations, c);
}
};
Run Code Online (Sandbox Code Playgroud)
这样做的好处是不重复类的名称,整个事物可以隐藏在一个宏中(例如一个被调用的宏DECLARE_LOOP_INTERFACE).有没有办法在没有BOOST的情况下在C++ 03或更早版本中执行此操作?我将在Windows/Linux/Mac上使用该代码.
我知道语法很难看,它是一个研究代码.请不要介意.
我相信这应该有效:
template <class Param, class Loop>
void Dispatcher(Loop *loop_valueIsNotUsed, int iterations, Context c)
{
MyUtility<Loop>::template WrapLoop<Param>(iterations, c);
}
// Usage:
struct MyLoop
{
template <class Param>
void Run(int iterations, Context c)
{
Dispatcher<Param>(this, iterations, c);
}
};
Run Code Online (Sandbox Code Playgroud)
Loop 将从电话中推断出来.
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |