首页 > 解决方案 > C++ `using {var}` 不是 {child class} 的成员 - 在 MSVC 或 Clang 中使用`std::deque` 时

问题描述

下面的代码error C2039: 'value_type': is not a member of 'Child_Container'在第 7 行给出了错误。这发生在 MSVC 和 Clang 中,但不会发生在 GCC 中。从而在使用时std::deque,而不是std::setstd::vector。有谁知道为什么?谢谢!

#include <deque>

template<typename T_Container>
struct _View
{
    using           NOPE = typename T_Container::value_type;
};

template<typename T_type, typename T_Self>
struct Base_Container
{
    using value_type = T_type;
    std::deque<_View<T_Self>>   _views;
};

struct Child_Container : public Base_Container<double, Child_Container>
{
    // using value_type = double; // Even with this line.
    using base = Base_Container<value_type, Child_Container>;
};

int main()
{
    return 0;
}

标签: c++templates

解决方案


这里的变量只是实例化时是否std::deque要求其元素类型完整。(当然,当某些成员函数被实例化时,它必须是完整的,但这是分开的。)如果是这样,你最终需要value_type在它被声明之前,这会产生观察到的错误。C++17 要求std::vector支持不完整类型,但没有提及std::deque,这就是每个标准库不同的原因。


推荐阅读