我遇到的问题是我无法用一个小的自包含示例重新制作。也就是说,我写了这样一个例子(如下所示),但是小例子编译,而实际代码没有。
我在基地定义了一个功能模板:
template<class ExplicitSpace>
struct ExplicitState {
...
template<class Neighbor>
std::vector<Neighbor> successors() const {...}
...
};
我在派生类型中有以下内容:
template <typename CostType_>
struct GridMapState: ExplicitState<GridMap<CostType_>> {
using Base = ExplicitState<GridMap<CostType_>>;
using Base::Base; // inherit constructors
using Base::successors; // this is supposed to do the trick
...
std::vector<Neighbor> successors() const {
// return static_cast<const Base *>(this)->template successors<Neighbor>();
// the above commented version compiles
return successors<Neighbor>(); // this does not compile
}
...
};
正如评论所示,我可以明确地调用基座的successors()
,但我不能依靠using Base::successors;
使其可见。
gcc 4.8.2。给出以下内容:
GridMapState.h: In member function ‘std::vector<StateNeighbor<GridMapState<typename ExplicitState<GridMap<CostType_> >::CostType> > > GridMapState<CostType_>::successors() const’:
GridMapState.h:20:35: error: expected primary-expression before ‘>’ token
return successors<Neighbor>();
以下是试图重新产生问题的小例子,但编译得很好:
template <typename U>
struct A {
template <typename T>
T f() {return T(0);}
};
template <typename V>
struct B : A<int> {
using MyT = int;
using Base = A<int>;
using Base::f;
MyT f() {return f<MyT>();}
};
int main()
{
B<int> b;
std::cout << b.f() << std::endl;
return 0;
}