首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析是如何工作的,或者是什么使类型完整或不完整?

解析是如何工作的,或者是什么使类型完整或不完整?
EN

Stack Overflow用户
提问于 2016-09-19 14:38:34
回答 1查看 78关注 0票数 2

请考虑以下几点:

代码语言:javascript
复制
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;
}

请查看注释中表示为"version 2“的Astar类定义中的行。当Astar像在main中一样被实例化时,成员g_的类型是GeneratorPolicy<Astar>,它的成员h_类型是Heuristic的实例化。但是,似乎Neighbor别名在Heuristic中的声明应该要求GeneratorPolicy<Astar>是完整的。我认为它还没有完成,因为编译器现在正在解析它。因此,我对代码为什么会编译感到困惑。

如果您回答GeneratorPolicy<Astar>已经完成,那么请解释为什么版本1没有编译。该版本的g++ 5.4.0的错误输出是:

代码语言:javascript
复制
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 {

编辑:多亏了Amadeus,这里有一个更简单的版本:

代码语言:javascript
复制
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版编译得很好。

EN

回答 1

Stack Overflow用户

发布于 2016-09-19 17:18:52

您的代码很难理解。所以,我做了一个更简单的版本:

代码语言:javascript
复制
template <typename T>
struct Bar;

template <typename T>
struct Foo
{
    using a = typename Bar<T>::Type;
};

template <typename T>
struct Bar
{
    using Type = int;
    Foo<T> x;
};

int main()
{
    //Foo<int> x;    // Version 1 - compile error
    Bar<int> x;  // Version 2 - compile fine
    (void)x;
}

当您想要在定义完某个类型之前使用它时,就会发生不完全类型。在版本1中,您正在尝试定义Foo<int>,同时希望在Bar<int>中使用它。

在版本2中,定义Bar<int>,然后定义仅使用Bar<int>::TypeFoo<int>,这很容易完成定义。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39575841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档