Reputation: 1233
So I have a class like this
template <typename T>
class Foo{
public:
Foo(T& outputProcessor):
_output(outputProcessor){}
protected:
void processData(int data){
std::map<std::string,int> output;
//T(output); ??
}
T& _output;
};
And then I'm creating it:
Foo< decltype (processOutput) >(processOutput)
processOutput
is defined as
std::function<void(std::map<std::string, int>)> processOutput;
Can I call T
from Foo
, and if yes, how?
Upvotes: 1
Views: 333
Reputation: 50540
As an alternative implementation, you can inherit privately from T
as it follows:
template <typename T>
class Foo: private T {
public:
Foo(T outputProcessor):
T(std::move(outputProcessor)) { }
protected:
void processData(int data) {
std::map<std::string,int> output;
(*this)(output);
// Or: T::operator()(output);
}
};
This way, the outputProcessor
policy becomes part of the Foo
class, as it ought to be.
You can still construct an instance of Foo
as it follows:
int main() {
std::function<void(std::map<std::string, int>)> processOutput = [](std::map<std::string, int>){};
Foo< decltype (processOutput) > foo(processOutput);
}
Or using directly the lambda function:
int main() {
auto processOutput = [](std::map<std::string, int>){};
Foo< decltype (processOutput) > foo(processOutput);
}
No std::function
is actually required to do that.
Upvotes: 3
Reputation: 48938
You can't call T
, T
is a type. You can call an instance of it, yes.
In your case, _output
is an instance of T
, so you can call it like a normal function:
_output(output); //Call function in '_output' and passes 'output' as parameter.
Upvotes: 1