相关疑难解决方法(0)

为什么C++ STL不提供任何"树"容器?

为什么C++ STL不提供任何"树"容器,而最好使用什么?

我想将对象的层次结构存储为树,而不是使用树作为性能增强...

c++ tree stl

362
推荐指数
13
解决办法
19万
查看次数

如何将不完整类型用作矢量模板参数?

TIL以下程序是合法的以及诸如此类的:

#include <vector>

struct Bar;

struct Foo
{
    using BarVec = std::vector<Bar>::size_type;
};

struct Bar {};

int main()
{
   Foo f;
}
Run Code Online (Sandbox Code Playgroud)

怎么样?Bar是一个不完整的类型,因此编译器无法知道std::vector<Bar>它是什么,或者它包含一个成员size_type,或者该成员size_type是一个类型.

我能想出的唯一解释是,任何假设的专业化(可能)都必须已经在范围内导致size_type与"基础"模板定义中给出的含义不同,并且size_type不是从属名称(两者都是有助于编译器确定性的因素).

这里的法律理由是什么?

c++ language-lawyer c++11

19
推荐指数
1
解决办法
3276
查看次数

为什么C++容器不允许不完整的类型?

为什么 C++不允许实例化不完整类型的容器?

编写没有这个限制的容器当然是可能的 - boost :: container完全能够做到这一点.据我所知,它似乎没有给出任何性能或其他类型的增益,但标准声明它是未定义的行为.

例如,它确实阻止了构建递归数据结构.

为什么C++标准会强加这种任意限制呢?尽可能允许不完整类型作为模板参数的缺点是什么?

c++ templates stl incomplete-type

16
推荐指数
1
解决办法
2992
查看次数

在自己的decle中使用类作为模板参数

(我知道这不是常规的实现,但是我想尝试一下。)

struct TrieNode {
    std::unordered_map<char, TrieNode> next;
};
Run Code Online (Sandbox Code Playgroud)

此类已很好地编译,并且在Visual Studio 2017下可以按预期工作。但是,它不使用gcc(c ++ 14)进行编译(正如我期望的那样)。

In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:1:
    /usr/include/c++/8/bits/stl_pair.h: In instantiation of ‘struct std::pair<const char, TrieNode>’:
    /usr/include/c++/8/bits/stl_vector.h:1610:27:   required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const char, TrieNode> >’
    /usr/include/c++/8/bits/hashtable_policy.h:234:43:   required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const char, TrieNode> >’
    /usr/include/c++/8/bits/hashtable_policy.h:280:12:   required from ‘struct std::__detail::_Hash_node<std::pair<const char, TrieNode>, false>’
    /usr/include/c++/8/bits/hashtable_policy.h:2027:49:   required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const char, TrieNode>, false> > >’
    /usr/include/c++/8/bits/hashtable.h:173:11:   required from ‘class std::_Hashtable<char, std::pair<const char, TrieNode>, std::allocator<std::pair<const char, …
Run Code Online (Sandbox Code Playgroud)

c++ gcc nested forward-declaration dependent-type

3
推荐指数
1
解决办法
69
查看次数