dr11
dr11

Reputation: 5726

Usage of template class with template method in C++

I have a class, which have a public templated methods. This class has 2 strategies of behavior, which i want to pass via class template.

template<class Strategy>
class SomeClass {
public:
    template<class B>
    void ProcessType(){}
};

// And do something like this:
SomeClass<Strategy1> sc();
sc.ProcessType<SomeClassType>();
sc.ProcessType<SomeClassType2>();

SomeClass<Strategy2> sc2();
sc2.ProcessType<SomeClassType>();
sc2.ProcessType<SomeClassType2>();

But this code doesn't compile. I need to keep usage exact like this (to manipulate just via strategy).

Upvotes: 1

Views: 87

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

This is the problem:

SomeClass<Strategy1> sc();

That is a declaration of a function called sc that takes no arguments and returns a SomeClass<Strategy1>. This is commonly known as a vexing parse (but not the most vexing parse). What you want is:

SomeClass<Strategy1> sc;

Upvotes: 4

Related Questions