在自己的声明中使用 class 作为模板参数
Using a class as template parameter in its own decleration
(我知道这不是常规实现,但我想尝试这个想法。)
struct TrieNode {
std::unordered_map<char, TrieNode> next;
};
这个 class 编译得很好,在 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, TrieNode> >, std::__detail::_Select1st, std::equal_to<char>, std::hash<char>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >’
/usr/include/c++/8/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<char, TrieNode>’
prog.cpp:9:37: required from here
/usr/include/c++/8/bits/stl_pair.h:215:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
_T2 second; /// @c second is a copy of the second object
^~~~~~
prog.cpp:7:8: note: forward declaration of ‘struct TrieNode’
struct TrieNode {
^~~~~~~~
我想知道visual c++实现怎么没问题?标准对此类情况有何规定?
我无法告诉您为什么它在 MSVS2017 中有效,但根据标准,使用 std::unordered_map
的不完整类型是未定义的行为。 class 的 complete-class context 由 [class.mem]/6 定义为
A complete-class context of a class is a
function body ([dcl.fct.def.general]),
default argument,
noexcept-specifier ([except.spec]), or
default member initializer
只有在那些地方,class 名称才表示一个完整的类型。由于我们不在任何这些地方,因此名称命名为不完整的类型。如果我们检查我们是否可以将它与 std::unordered_map
一起使用,我们检查 [res.on.functions]/2 并且我们有
In particular, the effects are undefined in the following cases: [...]
- If an incomplete type ([basic.types]) is used as a template argument when instantiating a template component or evaluating a concept, unless specifically allowed for that component.
所以一般来说这是不允许的。我们需要检查 [container.requirements] and [unord.map] 但其中没有任何内容表明允许不完整的类型。这意味着我们退回到一般规则,这是不允许的。
(我知道这不是常规实现,但我想尝试这个想法。)
struct TrieNode {
std::unordered_map<char, TrieNode> next;
};
这个 class 编译得很好,在 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, TrieNode> >, std::__detail::_Select1st, std::equal_to<char>, std::hash<char>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >’ /usr/include/c++/8/bits/unordered_map.h:105:18: required from ‘class std::unordered_map<char, TrieNode>’ prog.cpp:9:37: required from here /usr/include/c++/8/bits/stl_pair.h:215:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type _T2 second; /// @c second is a copy of the second object ^~~~~~ prog.cpp:7:8: note: forward declaration of ‘struct TrieNode’ struct TrieNode { ^~~~~~~~
我想知道visual c++实现怎么没问题?标准对此类情况有何规定?
我无法告诉您为什么它在 MSVS2017 中有效,但根据标准,使用 std::unordered_map
的不完整类型是未定义的行为。 class 的 complete-class context 由 [class.mem]/6 定义为
A complete-class context of a class is a
function body ([dcl.fct.def.general]),
default argument,
noexcept-specifier ([except.spec]), or
default member initializer
只有在那些地方,class 名称才表示一个完整的类型。由于我们不在任何这些地方,因此名称命名为不完整的类型。如果我们检查我们是否可以将它与 std::unordered_map
一起使用,我们检查 [res.on.functions]/2 并且我们有
In particular, the effects are undefined in the following cases: [...]
- If an incomplete type ([basic.types]) is used as a template argument when instantiating a template component or evaluating a concept, unless specifically allowed for that component.
所以一般来说这是不允许的。我们需要检查 [container.requirements] and [unord.map] 但其中没有任何内容表明允许不完整的类型。这意味着我们退回到一般规则,这是不允许的。