将仅标头库的包含包装在单个 cpp 文件中
Wrap the includes of a header-only library in a single cpp file
当我使用像 Boost Spirit 这样的大型仅标题库时,我经常想将其包含隔离到单个 cpp 文件中(parser.cpp
), 为了将所需的编译时间减少到只有一个文件。目标是当源文件(main.cpp
)需要Spirit设施时, 它可以包含一个头文件,该文件呈现一个不引用任何 Spirit 内容的前端类,让这个源文件快速编译。 只有一个 cpp 文件 (parser.cpp
) 需要包含 Spirit 标头,并且需要更多时间来编译。
但是,如何创建一个内部需要精神的类,而不在其声明中引用它呢? 我发现的唯一方法是使用指向不完整包装类(QiParser
)的指针成员,然后在内部使用new
和delete
以创建真正的解析器。
我的问题是:有没有更好的方法来做到这一点,而不使用new
和delete
?
这是我现在使用的代码类型:
主.cpp
#include <iostream>
#include "parser.h"
int main (int, char **)
{
Parser p;
int n = 0;
p.parse("78", n);
std::cout << "The parsed number is " << n << std::endl;
return 0;
}
解析器.h
#include <string>
class QiParser; //< Incomplete class declaration
class Parser {
public:
Parser();
~Parser();
bool parse(const std::string &s, int &result) const;
private:
QiParser *qi_parser; //< The only way I found to not include
// the Spirit headers in this file
};
解析器.cpp
#include "parser.h"
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
// The QiParser class is only present to solve my problem;
// I would like to put this code directly into the Parser class, but without showing
// any Spirit stuff inside the parser.h header file
struct QiParser : qi::grammar<std::string::const_iterator, int()>
{
QiParser() : base_type(start)
{
start %= qi::int_;
}
qi::rule<std::string::const_iterator, int()> start;
};
// Implementation of the Parser class:
Parser::Parser()
{
qi_parser = new QiParser;
}
Parser::~Parser()
{
delete qi_parser;
}
bool Parser::parse(const std::string &s, int &result) const
{
auto iter = s.begin(), end = s.end();
return qi::parse(iter, end, *qi_parser, result);
}
是的,使用std::unique_ptr<QiParser> qi_parser;
而不是QiParser * qi_parser;
。这被称为 PImpl 习语
相关文章:
- 编辑单个 YAML 值,而不更新 YAML (YAML CPP) 其余部分的格式
- 生成main.c和main.cpp的单个Makefile
- 从单个.cpp构建共享对象
- SystemC:单个 cpp 文件中的多个模块实现
- 将仅标头库的包含包装在单个 cpp 文件中
- 在单个makefile目标中同时编译C和CPP源
- CPP中的单个参考代表两种或多个数据类型
- 构建一个可执行文件,如果是单个 cpp 文件
- CPP / JUCE 音频编程框架 - 滑块数组不起作用 - 单个滑块工作
- 单个CPP文件中多次声明函数声明可以吗?
- 在Visual Studio 2015中运行单个.cpp文件
- CPP 3D 动态创建由单个内存块组成的 3D 数组
- qmake:将单个cpp文件编译为可执行文件
- 如何使用 clang 获取单个 cpp 文件的 AST
- 多个main.cpp文件的单个make文件
- 有没有办法在Visual Studio Express '12项目中编译和运行单个.cpp文件?
- CMake:如何禁用单个*.cpp文件的优化
- 如何使用单个makefile编译不同目录下的多个.cpp文件
- 如何减少单个.cpp文件的大型c++库的编译时间
- 一个宏的重定义可以应用到单个cpp文件吗?