简体   繁体   English

将代码从C++11转换为C++98的策略?

[英]Strategy of converting code from C++11 to C++98?

I have a project written in C++11.我有一个用 C++11 编写的项目。 As C++11 is widely accepted, the project works well up to now, until I have to run it in a environment which not support C++11.由于 C++11 被广泛接受,该项目到目前为止运行良好,直到我必须在不支持 C++11 的环境中运行它。

I have already used std::shared_ptr , auto , std::intializer_list , nullptr enum class , chrono , std::vector<int> x{1,2,3,4} and many other C++11 technique everywhere in the project.我已经在项目中到处使用了std::shared_ptrautostd::intializer_list intializer_list 、 nullptr enum classchronostd::vector<int> x{1,2,3,4}和许多其他 C++11 技术。

How can I move the big project in C++11 to C++98?如何将 C++11 中的大项目移动到 C++98?

I apologize if it is a stupid problem, Thanks for your time.如果这是一个愚蠢的问题,我深表歉意,感谢您的宝贵时间。

Decide about each piece separately.分别决定每一件。

If that platform can run at least a subset of boost , you can probably just replace std::chrono and std::shared_ptr .如果该平台至少可以运行boost的一个子集,您可能只需替换std::chronostd::shared_ptr You can also replace lambdas with BOOST_LOCAL_FUNCTION你也可以用BOOST_LOCAL_FUNCTION替换 lambdas

I had to do something similar, but my challenge was co convert it from C++17 to C which was present on customer CentOS 7 (they used custom CentOS Image, and we were not allowed to install any new packages). I had to do something similar, but my challenge was co convert it from C++17 to C which was present on customer CentOS 7 (they used custom CentOS Image, and we were not allowed to install any new packages).

What I did was to throw out step by step all dependencies which used C++ elements and replace them with C variants.我所做的是逐步丢弃所有使用 C++ 元素的依赖项,并用 C 变体替换它们。 It took some time but it worked.这花了一些时间,但它奏效了。 Also looked after some other solution, but I was not able to find any.还关注了其他一些解决方案,但我找不到任何解决方案。

You could always rewrite shared pointer, vector etc. in C.您总是可以在 C 中重写共享指针、向量等。

Maybe not exactly what you ask but when I want to run code to another computer without support C++11 I will create docker with C++11 inside this computer.也许不完全是你问的,但是当我想在不支持 C++11 的情况下在另一台计算机上运行代码时,我将在这台计算机内创建 docker 和 C++11。 Hope this helps!希望这可以帮助!

One solution is to try looking for C++ external libraries such as Boost and Qt, which could cover half of your requisites.一种解决方案是尝试寻找 C++ 外部库,例如 Boost 和 Qt,它们可以满足您一半的需求。

However some things are implementation-specific and upto you, such as the auto keyword, which you may choose to not use.但是,有些事情是特定于实现的,取决于您,例如您可以选择不使用的auto关键字。 (In fact if your using a proper debugger cum IDE, you'll get to check even complex data types in an instance) (事实上,如果您使用适当的调试器和 IDE,您甚至可以在实例中检查复杂的数据类型)

Another alternative to that is replacing auto by templates, as pointed out here .另一种替代方法是用模板替换auto ,正如在此处指出的那样。 (without boost) (没有升压)

Similarly, stuff like macros and variadic templates shouldn't be hard to cover.同样,宏和可变参数模板之类的东西也不难涵盖。

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

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