Ghita
Ghita

Reputation: 4505

Template class typedef - use outside of class

I have a class like this:

struct WorkItemResultData;

template <typename ExecutionPolicy>
class Engine
{
public:
    typedef std::shared_ptr<WorkItemResultData> WorkItemResultData_ptr;
}

typedef does not depend on any template argument. Is there a way to use type Engine::WorkItemResultData_ptr outside of Engine class ?

EDIT I know I could use it like awoodland proposed the solution bellow but my typedef is independent of type arguments and want to be able to do it without specifying a specific Engine type.

Upvotes: 3

Views: 2114

Answers (2)

Is there a way to use type Engine::WorkItemResultData_ptr outside of Engine class ?

Yes, but you'll need to say typename if it's in a template context, e.g.:

template <typename T>
void foo() {
  typename Engine<T>::WorkItemResultData_ptr instance;
}

You can't access that typedef without a type. There are three workarounds possible though:

  1. typedef outside the template! - it's likely it doesn't have much to do with the template if it doesn't depend on the types.
  2. Use a bodge and refer to Engine<void>::WorkItemResultData_ptr.
  3. Have a non-template base class which contains the typedef and inherit from that. You can access the non-template base class fine then.

Upvotes: 4

Luc Touraille
Luc Touraille

Reputation: 82081

Since the typedef does not depend on Engine at all, you can pull it out of the class:

typedef std::shared_ptr<WorkItemResultData> WorkItemResultData_ptr;

template <typename ExecutionPolicy>
class Engine {...};

If you want to keep it encapsulated, simply use a namespace:

namespace Engine
{
    typedef std::shared_ptr<WorkItemResultData> WorkItemResultData_ptr;
}

Upvotes: 5

Related Questions