the swine
the swine

Reputation: 11031

C++ type of this

I'm writing some template code in C++ and I came to a point where it would make the code shorter / better / more usable if I could determine type of this. I do not want to use C++0x as the code is to be backward compatible with older compilers. I do not want to use BOOST either. What I have is 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 don't like having MyLoop in MyUtility template specialization. With C++0x, one can use something like:

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

That has an advantage of not repeating name of the class, the whole thing could be hidden in a macro (e.g. a one called DECLARE_LOOP_INTERFACE). Is there a way to do this in C++03 or older, without BOOST? I will be using the code on Windows / Linux / Mac.

I know the syntax is ugly, it is a research code. Please, do not mind that.

Upvotes: 4

Views: 169

Answers (1)

I believe this should work:

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 will be deduced from the call.

Upvotes: 6

Related Questions