我有模板类Reader
template<typename T>
class Reader{
typedef T type;
};
特殊实现(派生类)具有带有签名的方法
T read(IStream&, any number of arguments, zero possible)
即类 IntegerReader
公共功能:
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);
}
现在我想创建一个包装器,这将允许我创建另一个阅读器,并使用参数调用一些阅读器。
我试过这个:
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
这个:
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
和这个:
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’
g++-4.7给出的编译错误
虽然我不确定第一个示例是否正确并且应该编译,但我相信第二个和第三个应该。
我发现了这个错误,似乎没有修复。
有解决方法吗,我该怎么做?