I have template class Reader
template<typename T>
class Reader{
typedef T type;
};
Special implementations (derrived classes) have methods with signature
T read(IStream&, any number of arguments, zero possible)
i.e class IntegerReader
public functions:
template <typename T>
class IntegerReader : public Reader<T>{
public:
T read(IStream& stream);
T read(IStream& stream, T min, T max);
T read(IStream& stream, T min, T max, std::string name);
}
Now I want to create a wrapper, that will allow me create another reader, with will call some reader with arguments.
I've tried this:
template <typename T, typename... Args>
class ParametrizedReader : public Reader<typename T::type> {
T reader;
Args... args;
ParametrizedReader(T reader, Args... args):reader(reader), args(args){
}
typename T::type read(IStream& stream){
return reader.read(args..., stream);
}
};
testlib/readerWrapper.hpp:7:6: error: expected unqualified-id before ‘...’ token
testlib/readerWrapper.hpp: In constructor ‘ParametrizedReader<T, Args>::ParametrizedReader(T, Args ...)’:
testlib/readerWrapper.hpp:8:61: error: class ‘ParametrizedReader<T, Args>’ does not have any field named ‘args’
testlib/readerWrapper.hpp: In member function ‘typename T::type ParametrizedReader<T, Args>::read(IStream&)’:
testlib/readerWrapper.hpp:12:22: error: ‘args’ was not declared in this scope
this:
template <typename T, typename... Args>
class ParametrizedReader : public Reader<typename T::type> {
std::function<T()> lambda;
ParametrizedReader(T reader, Args... args){
lambda = [=](IStream& stream){
reader.read(stream, args...);
};
}
typename T::type read(IStream& stream){
return lambda(stream);
}
};
testlib/readerWrapper.hpp:9:24: error: parameter packs not expanded with ‘...’:
testlib/readerWrapper.hpp:9:24: note: ‘args’
testlib/readerWrapper.hpp:9:28: error: expansion pattern ‘args’ contains no argument packs
and this:
template <typename T, typename... Args>
class ParametrizedReader : public Reader<typename T::type> {
std::function<T()> lambda;
ParametrizedReader(T reader, Args... args){
lambda = [reader, args...](IStream& stream){
reader.read(stream, args...);
};
}
typename T::type read(IStream& stream){
return lambda(stream);
}
};
testlib/readerWrapper.hpp:8:25: error: expected ‘,’ before ‘...’ token
testlib/readerWrapper.hpp:8:25: error: expected identifier before ‘...’ token
testlib/readerWrapper.hpp:8:28: error: parameter packs not expanded with ‘...’:
testlib/readerWrapper.hpp:8:28: note: ‘args’
Compilation errors given by g++-4.7
While I'm not sure the first example is correct and should compile, I believe the second and third should.
I found this bug, which seems to be not fixed.
Is there workarounds, how can I do what I want?