考虑以下几点:解析是如何工作的或是什么使得一个类型完整或不完整?
template <typename Alg> class AlgorithmTraits;
template <class Alg>
struct Heuristic {
using Neighbor = typename AlgorithmTraits<Alg>::Generator::Neighbor;
};
template <class Alg, template <class> class HType>
struct Generator {
using Neighbor = int;
HType<Alg> h_;
};
template <class Alg>
using GeneratorPolicy = Generator<Alg, Heuristic>;
template <template <class> class InitialHeuristic_,
template <class> class Generator_> class Astar;
template <template <class> class InitialHeuristic_,
template <class> class Generator_>
struct AlgorithmTraits<Astar<InitialHeuristic_, Generator_>> {
using MyAlgorithm = Astar<InitialHeuristic_, Generator_>;
using InitialHeuristic = InitialHeuristic_<MyAlgorithm>;
using Generator = Generator_<MyAlgorithm>;
};
template <template <class> class InitialHeuristic_,
template <class> class Generator_>
class Astar {
using InitialHeuristic = typename AlgorithmTraits<Astar>::InitialHeuristic;
using Generator = typename AlgorithmTraits<Astar>::Generator;
//InitialHeuristic h_; // version 1 (does not compile)
Generator g_; // version 2 (compiles)
};
int main() {
Astar<Heuristic, GeneratorPolicy> a; (void)a;
return 0;
}
请看行记为“2版”所评论的Astar
类的定义。当Astar
被例示为如在main
中时,成员g_
是类型GeneratorPolicy<Astar>
,其具有成员h_
的类型,其是Heuristic
的实例化。但是,似乎Heuristic
中Neighbor
别名的声明应该要求完成GeneratorPolicy<Astar>
。我认为它不完整,因为编译器现在正在解析它。因此,我对代码编译的原因感到困惑。
P.S.如果你回答GeneratorPolicy<Astar>
已完成,请解释版本1如何不编译。的g++ 5.4.0
该版本的错误输出是:
temp.cpp: In instantiation of ‘struct Generator<Astar<Heuristic, GeneratorPolicy>, Heuristic>’:
temp.cpp:17:72: required from ‘struct Heuristic<Astar<Heuristic, GeneratorPolicy> >’
temp.cpp:43:22: required from ‘class Astar<Heuristic, GeneratorPolicy>’
temp.cpp:48:39: required from here
temp.cpp:23:16: error: ‘Generator<Alg, HType>::h_’ has incomplete type
HType<Alg> h_;
^
temp.cpp:16:8: note: declaration of ‘struct Heuristic<Astar<Heuristic, GeneratorPolicy> >’
struct Heuristic {
编辑:由于艾玛迪斯,这里是一个简单的版本:
template <typename Alg>
struct Generator;
template <typename Alg> struct Heuristic {
using Neighbor = typename Generator<Alg>::Neighbor;
};
template <typename Alg> struct Generator {
using Neighbor = int;
Heuristic<Alg> h;
};
int main()
{
Heuristic<int> x; // Version 1 - compile error
//Generator<int> x; // Version 2 - compile fine
(void)x;
}
不过,我还是不清晰明白为什么第2版编译精细。
您的代码非常复杂,因此难以阅读和掌握。试着用一个更简单的例子来重现你的问题 – sigy
“不完整类型”通常是一种类型,比如'class'或'struct',它只有一个声明而没有定义,就像'class Incomplete;'一样。 –