C ++ type of this

I am writing template code in C ++ and I have come to a point where this would make the code shorter / better / more usable if I could determine the type this. I do not want to use C ++ 0x, since the code must be backward compatible with older compilers. I also do not want to use BOOST. I have something like:

struct MyLoop {
    template <class Param>
    void Run(int iterations, Context c)
    {
        MyUtility<MyLoop>::template WrapLoop<Param>(iterations, c);
    }
};

This can be used for some interesting loop optimizations. I do not like to have MyLoopin the MyUtilitytemplate specialization. With C ++ 0x, you can use something like:

struct MyLoop {
    template <class Param>
    void Run(int iterations, Context c)
    {
        MyUtility<decltype(*this)>::template WrapLoop<Param>(iterations, c);
    }
};

This has the advantage that the class name is not repeated, the whole thing can be hidden in the macro (for example, one is called DECLARE_LOOP_INTERFACE). Is there a way to do this in C ++ 03 or later, without BOOST? I will use the code on Windows / Linux / Mac.

, , . , .

+4
1

, :

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);
  }
};

Loop .

+6

All Articles