使用带有奇怪重复模板模式的关键字时出错

not*_*orb 2 c++

有谁知道为什么下面的代码编译找不到N::balance_type

我正在使用 Stroustrup C++ 4th Ed Page 771 中的这个例子。它是奇怪的重复模板模式

class Bal {};

template<typename N>
struct Node_base : N::balance_type {
};

template<typename Val, typename Balance>
struct Search_node : public Node_base<Search_node<Val,Balance>> {
    using balance_type = Balance;
};

int main()
{
    Search_node<int, Bal> sn;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

汇编:

clang++ -std=c++11 -Wall -pedantic test197.cc && ./a.out
test197.cc:4:23: error: no type named 'balance_type' in 'Search_node<int, Bal>'
struct Node_base : N::balance_type {
                   ~~~^~~~~~~~~~~~
test197.cc:8:29: note: in instantiation of template class
      'Node_base<Search_node<int, Bal> >' requested here
struct Search_node : public Node_base<Search_node<Val,Balance>> {
                            ^
test197.cc:14:27: note: in instantiation of template class
      'Search_node<int, Bal>' requested here
    Search_node<int, Bal> sn;
                          ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

Łuk*_*zyk 5

g++表示SearchNode当您从Node_base<Search_node<Val,Balance>>

test.cpp: In instantiation of ‘struct Node_base<Search_node<int, Bal> >’:
test.cpp:8:8:   required from ‘struct Search_node<int, Bal>’
test.cpp:14:25:   required from here
test.cpp:4:8: error: invalid use of incomplete type ‘struct Search_node<int, Bal>’
 struct Node_base : N::balance_type {
        ^~~~~~~~~
test.cpp:8:8: note: declaration of ‘struct Search_node<int, Bal>’
 struct Search_node : public Node_base<Search_node<Val,Balance>> {
Run Code Online (Sandbox Code Playgroud)

我想在这个地方使用SearchNode是无效的,因为你现在正在定义它。