C++ 将类型映射到它的头文件

标签 c++ types header xtext

<分区>

我目前正在开发一种用于轻松创建 C++ 项目的简单编程语言。它允许您输入一些类似于 C++ 的简短代码并自动生成 .h 和 .cpp 文件。

我需要一些方法来映射类型,例如从标准库,到它相应的头文件。这使得仅使用我的语言中的类型并自动推断要包含在生成的代码中的 header 成为可能。

这是当前语言的示例:

class Car {
    std::string print()

    members:
        int i, j
        std::array<Wheel, 4> wheels
}

在处理这段代码时,我发现类型 std::stringstd::array 需要映射到它们的头文件。 我怎样才能实现这种映射?

是否有公开可用的数据库?我当然不想自己解析所有 header (我假设 IDE 是这样做的)。

除了仅支持标准库之外,能够支持其他库当然也很有用,但这只是次要目标。

最佳答案

好吧,您可以使用一些优秀的引用资料来构建自己的数据库。由于标准变化缓慢,您可以使用引用资料为它们生成 map 。这里有个好c++ reference

很难简单地在头文件中四处搜索,因为它们通常是使用您不想在代码中使用的后端位构建的,并且您无法区分它们。

如果我要构建它,我会使用标准库引用构建基本 map ,而不是通过头文件进行解析;例如,在上面的 Car 示例中,您应该 #include <string>正确的?

如果你要解析头文件<string>对于给定的编译器,您可能会发现它仅由后端位组成,而后端位又包含其他文件;

在 g++ 版本 4.6 中 <string>看起来像这样:

// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
// 2005, 2006, 2007, 2009, 2010, 2011
// Free Software Foundation, Inc.
// GPL version 3.1 or later ... etc...

#ifndef _GLIBCXX_STRING
#define _GLIBCXX_STRING 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <bits/stringfwd.h>
#include <bits/char_traits.h>  // NB: In turn includes stl_algobase.h
#include <bits/allocator.h>
#include <bits/cpp_type_traits.h>
#include <bits/localefwd.h>    // For operators >>, <<, and getline.
#include <bits/ostream_insert.h>
#include <bits/stl_iterator_base_types.h>
#include <bits/stl_iterator_base_funcs.h>
#include <bits/stl_iterator.h>
#include <bits/stl_function.h> // For less
#include <ext/numeric_traits.h> 
#include <bits/stl_algobase.h> 
#include <bits/range_access.h>
#include <bits/basic_string.h>
#include <bits/basic_string.tcc> 

#endif /* _GLIBCXX_STRING */

问题现在变得很明显了。例如是<string>规范 std::ios_base ? 想了很多,以后可能会有一些其他的想法。

关于C++ 将类型映射到它的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624725/

相关文章:

c++ - 可变参数函数和可变参数模板重载查找

c# - 比较通用接口(interface)类型

header - 蝗虫传递标题

c++ - 包括<Windows.h>导致(未知属性“no_init_all”)错误

c++ - 如果返回图像,我需要 cvReleaseImage 吗?

c++ - 读取字符输入和循环计数器的问题

c++ - 使用 std::set_intersection 时,哪个集合用于将值复制到目标集合中?

python - Great Expectations expect column to contain only integers 当只有一个是坏的时,所有行都失败

64-bit - Lua是否使用64位整数?

javascript - 未处理的 promise 拒绝: Can't set headers after they are sent