Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何组织只包含头的库的include,该库希望公开相互递归的函数?_C++_Include - Fatal编程技术网

C++ 如何组织只包含头的库的include,该库希望公开相互递归的函数?

C++ 如何组织只包含头的库的include,该库希望公开相互递归的函数?,c++,include,C++,Include,我正在创建一个库,其中有如下函数: template <typename T> bool A (int, int, T&); template <typename T> bool B (int, int, T&); template <typename T> class IFoo { virtual T funct(int i) = 0; }; template <typename T> class FooA : IF

我正在创建一个库,其中有如下函数:

template <typename T> bool A (int, int, T&);
template <typename T> bool B (int, int, T&);

template <typename T> 
class IFoo {
    virtual T funct(int i) = 0;
};

template <typename T>
class FooA : IFoo<T> {
    virtual T funct(int i) override {
        if (!i) {
            return FooB(i-1);
        } else {
            return T(0);
        }
    }
};

template <typename T>
class FooB : IFoo<T> {
    virtual T funct(int i) override {
        if (!i) {
            return FooA(i-1);
        } else {
            return T(1);
        }
    }
};
模板bool A(int,int,T&);
模板布尔B(int,int,T&);
模板
类IFoo{
虚T函数(int i)=0;
};
模板
类FooA:IFoo{
虚T函数(int i)重写{
如果(!i){
返回FooB(i-1);
}否则{
返回T(0);
}
}
};
模板
类FooB:IFoo{
虚T函数(int i)重写{
如果(!i){
返回FooA(i-1);
}否则{
返回T(1);
}
}
};
这些定义要求将代码拆分为声明和定义部分,以便允许两个过程相互了解,然后同时访问实现


但我的问题是,我如何能够以清晰的方式公开这些函数(也就是说,我应该如何编写一个头来封装依赖项包含模式),同时使添加新的“函数”或其他相互递归的组件(也就是说,函数或派生类)变得容易这样,组件的用户就不必手动排序声明头和定义头的包含?

将强连接的组件放在一个头中

将强连接的组件保持在一个标题中

将声明与实现分离

template <typename T>
class IFoo {
    virtual T funct(int i) = 0;
};

template <typename T>
class FooA : IFoo<T> {
    virtual T funct(int i) override;  // declaration only...
};

template <typename T>
class FooB : IFoo<T> {
    virtual T funct(int i) override {
        if (!i) {
            return FooA<T>(i-1);
        } else {
            return T(1);
        }
    }
};


// implementation here - once all type information is available
template<typename T>
T FooA<T>::funct(int i) {
    if (!i) {
        return FooB<T>(i-1);
    } else {
        return T(0);
    }
}

通过将声明与实现分离

template <typename T>
class IFoo {
    virtual T funct(int i) = 0;
};

template <typename T>
class FooA : IFoo<T> {
    virtual T funct(int i) override;  // declaration only...
};

template <typename T>
class FooB : IFoo<T> {
    virtual T funct(int i) override {
        if (!i) {
            return FooA<T>(i-1);
        } else {
            return T(1);
        }
    }
};


// implementation here - once all type information is available
template<typename T>
T FooA<T>::funct(int i) {
    if (!i) {
        return FooB<T>(i-1);
    } else {
        return T(0);
    }
}

那个密码似乎不对
FooB
FooA
通常不能转换为
T
,是吗?@KerrekSB这只是表达问题的一个最小示例。真正的应用是表达式求值过程和递归下降解析函数。两者都需要函数之间的相互递归,因为表达式和非终端规则可以分别包含其他表达式和非终端规则。对于这些,我们分别使用带有多态接口的委托类和模板化的相互递归函数
FooB
FooA
通常不能转换为
T
,是吗?@KerrekSB这只是表达问题的一个最小示例。真正的应用是表达式求值过程和递归下降解析函数。两者都需要函数之间的相互递归,因为表达式和非终端规则可以分别包含其他表达式和非终端规则。对于这些,我们分别使用具有多态接口的委托类和模板化的相互递归函数。对,但我还要问的是如何组织每个部分的分离,以及如何将它们打包回最终的头。大约有10个相互递归的过程,另一个过程的渐进“添加”过程将如何完成?只需要一个提示。@VermillionAzure“分离”和“添加备份”是什么意思?您是说一个包含多个其他文件的include文件,还是一个生成代码的pre-make工具?我的意思是,“如何将必须使用其他声明和定义的声明和定义封装到它们自己的头文件中”,然后,“如何向这些函数的用户公开单个头,以隐藏相互递归函数/头所需的包含依赖模式的复杂性?“是的,但我也要问的是如何组织每个部分的分离,以及如何将它们打包到最终标题中。大约有10个相互递归的过程,另一个过程的渐进“添加”过程将如何完成?只需要一个提示。@VermillionAzure“分离”和“添加备份”是什么意思?您是说一个包含多个其他文件的include文件,还是一个生成代码的pre-make工具?我的意思是,“如何将必须使用其他声明和定义的声明和定义封装到它们自己的头文件中”,然后,“如何向这些函数的用户公开单个标头,以隐藏相互递归函数/标头所需的包含依赖关系模式的复杂性?”
#pragma once
#include "ifoo.hpp"

template <typename T>
class FooA : IFoo<T> {
    virtual T funct(int i) override;
};
#pragma once
#include "ifoo.hpp"

template <typename T>
class FooB : IFoo<T> {
    virtual T funct(int i) override;
};
#pragma once
#include "fooa.hpp"
#include "foob.hpp"
template<typename T>
T FooA<T>::funct(int i) {
    if (!i) {
        return FooB<T>(i-1);
    } else {
        return T(0);
    }
}
#pragma once
#include "fooa.hpp"
#include "foob.hpp"
template<typename T>
virtual T FooB<T>::funct(int i) override {
    if (!i) {
        return FooA<T>(i-1);
    } else {
        return T(1);
    }
}
#pragma once

#include "detail/ifoo.hpp"
#include "detail/fooa.hpp"
#include "detail/foob.hpp"
#include "detail/impl_fooa.hpp"
#include "detail/impl_foob.hpp"