【问题标题】:Resolving circularly dependant nested type specifiers解析循环依赖嵌套类型说明符
【发布时间】:2014-09-25 22:00:35
【问题描述】:

很简单,有什么办法让A引用B::value_type,B引用A::value_type?

struct B;

struct A {
    using value_type = int;
    value_type a;
    B::value_type b;
};

struct B {
    using value_type = int;
    value_type b;
    A::value_type a;
};

【问题讨论】:

    标签: c++ circular-dependency


    【解决方案1】:

    仅以非常骇人听闻的方式。

    template<int> struct Z
    {
        struct B;
    
        struct A {
            using value_type = int;
            value_type a;
            typename B::value_type b;
        };
    
        struct B {
            using value_type = int;
            value_type b;
            typename A::value_type a;
        };
    };
    
    using A = Z<0>::A;
    using B = Z<0>::B;
    

    【讨论】:

    • 那行得通,只需要使用声明的顺序交换最后两个。我不太确定它为什么会起作用。为什么顺序很重要?
    • 别管改顺序了,编译前肯定是第一次没保存。
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2018-08-24
    相关资源
    最近更新 更多