将旧版头文件用作c ++ 20模块 [英] Using legacy header files as c++20 modules

查看:70
本文介绍了将旧版头文件用作c ++ 20模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的旧代码转换为可作为c ++模块导入的内容.问题是我希望它仍然可以工作,并易于作为旧的标头/源代码版本进行维护.我该怎么做(如果可能的话).

I am in the process of converting my old code to something that is importable as c++-modules. The problem is that i would want it to still work and be easily maintained as the old header/source version. How do I do this (if possible).

是否可以创建一个导出标头内容的模块?(也可以使用其他任何可以维护旧的.cpp/.h文件和模块文件的解决方案)

Is it possible to create a module that export the content of a header? (Any other solution that lets you maintain old .cpp/.h files and module files is also accepted)

玩具示例:

// In vector.h
template <typename T>
struct Vector {
    T x, y;
}

// In .cppm
export module vector;
// #include "vector.h"
// Export struct/class Vector from header

我尝试的只是不同版本的 export Vector ,带有和不带有模板等.

What i have tried is just export Vector in different versions, with and without templates etc.

奖金问题:您可以为std lib执行此操作吗?(例如iostream或字符串)

Bonus question: Can you do this for the std lib? (for exmaple iostream, or string)

推荐答案

我将以其中之一为例我自己的库我已经转换为支持旧样式的标头和模块.

I will take examples from one of my own library I have converted to support both old style headers and modules.

该示例仅用于标头库,我将添加一个示例示例,该示例仅在拥有标头的情况下才不是标头.

This example is for a header only library, I will add an example of a library that is not header only when I have one.

使导出为可选.就我而言,我已经定义了文件宏,如果项目与模块一起使用,则可以添加导出.

Make export optional. In my case I have defined a file with macros that adds export if the project is used with modules.

export.h

#pragma once

// Macros for handling compatability with/without modules

#ifdef matmath_use_modules

#ifndef matmath_export
#define matmath_export export
#endif

#else

#ifndef matmath_export
#define matmath_export
#endif

#endif

然后标题库的定义几乎和往常一样,但是稍有调整,即使用export宏代替了想要的符号的export关键字,并删除了所有包含的符号.

Then the headers for the library will be defined almost as usual, but with the minor tweak that the export macro is used instead of the export keyword for symbols that you want, and all includes are removed.

vec.h

// vec.h
#pragma once

#include "export.h"

#ifndef matmath_use_modules

#include <cmath>
#include <ostream>
#if __cplusplus >= 201103L
#include <tuple>
#endif

#include "pi.h"

#endif

matmath_export template <typename T>
class VecT {
public:
    T x = 0, y = 0, z = 0;

    constexpr VecT() = default;

   ...
};

然后,您可以在所选模块中使用标题,并激活正确的宏.

Then you can use your header in a module of choice and activate the right macros.

vec.cppm

module;

#include <cmath>
#include <ostream>

import matmath.pi;

export module matmath.vec;

#define matmath_use_modules
#include "matmath/vec.h"

// If your file has a regular cpp-file you could include that
// here in the same fashion
// #include "path/to/vec.cpp"

最后:在使用类时,您可以选择通过模块文件或头文件使用项目(将lib的头文件和模块组合在一起会产生很多麻烦).

And finally: When using the class you can choose to use the project through your module files, or through the header files (combining headers and modules for the lib yields a lot of pain though).

如果您只想构建一个带有和不带有模块的小型项目,则只需删除所有与导出相关的语句并将导入语句转换为包含.这种方法需要您对代码进行更改,或者至少要执行一个单独的步骤来创建代码副本,而无需使用与模块相关的代码. https://github.com/mls-m5/rym/blob/master/non-module-build.sh

If you just want to build a small project with and without modules, you can just remove all export related statements and convert import statements to includes. This approach requires you to do changes to your code, or at least have a separate step that creates a copy of your code without the module related code. https://github.com/mls-m5/rym/blob/master/non-module-build.sh

这篇关于将旧版头文件用作c ++ 20模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆