【问题标题】:TS Concepts typename constraintTS Concepts 类型名约束
【发布时间】:2018-07-01 19:59:51
【问题描述】:

我正在尝试使用一个概念作为对子类的约束(由 gcc 使用 gnu2a 和 fconcepts 编译)来制作一个模板化继承的简单示例。我希望下面的示例可以正常编译,但我无法让它工作:

template<class structure>
concept bool Has_Type() {return requires{
    typename structure::type;
};}

template<class sub> requires Has_Type<sub>()
struct structure {
     //using type = typename sub::type;
};

struct child: structure<child> {
    using type = child;
};

这个概念抛出一个错误,说typename structure::type would be ill-formed。我不明白为什么,因为 child 具有 :: 运算符可以访问的类型。我尝试了这个例子来看看这个想法本身是否有效,并且编译并运行良好:

struct child {
    using type = child;
};

template<class it>
auto func() {
    typename it::type f = child();
    return 0;
}

// in a test.cpp file

auto g = func<child>();

这让我觉得这个想法得到了支持,所以我不确定为什么这个概念会失败。有人知道为什么吗?

【问题讨论】:

    标签: c++ templates inheritance typename c++-concepts


    【解决方案1】:

    这是因为 child 在那个时候是不完整的。 [class.mem]p6 说:

    类被认为是完全定义的对象类型(6.7)(或完整类型),在 类说明符。

    后面有一些例外(比如不在成员函数中)。但是在 base-clause 中,它是不完整的,因此成员 typeHas_Type 不可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      相关资源
      最近更新 更多