简体   繁体   English

将关键字与奇怪重复的模板模式一起使用时出错

[英]Error with using keyword with curiously recurring template pattern

Does anyone know why the following code compilation cannot find N::balance_type ?有谁知道为什么下面的代码编译找不到N::balance_type

I'm using this example from Stroustrup C++ 4th Ed Page 771. It is the curiously recurring template pattern .我正在使用 Stroustrup C++ 第 4 版第 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;
}

Compilation:汇编:

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.

g++ says SearchNode is an incomplete type when you derive from Node_base<Search_node<Val,Balance>>当您从Node_base<Search_node<Val,Balance>>派生时, g++SearchNode是不完整的类型

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>> {

I guess using SearchNode in this place is invalid because you are defining it right now.我猜在这个地方使用SearchNode是无效的,因为你现在正在定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM