简体   繁体   English

如何修改名称后如何恢复原始命名空间?

[英]How to revert back to an original namespace once its name is modified?

I'm using a library called stlport, which redefines the std namespace like this: 我正在使用一个名为stlport的库,它重新定义了std命名空间,如下所示:

# if defined (_STLP_REDEFINE_STD)
/*  We redefine "std" to "stlport", so that user code may use std:: transparently */
#   undef  std
#   define std STLPORT
# else

Because of this, where-ever in my code I try to use std::something , it gets substituted with _STLP::something . 因此,在我的代码中,我尝试使用std::something ,它被_STLP::something替换。

If I comment out the #define std STLPORT line, my legacy project's code won't compile (it's stlport dependent). 如果我注释掉#define std STLPORT行,我的遗留项目的代码将无法编译(它依赖于stlport)。

If I don't comment out the line, the new library I'm trying to add won't compile because it uses std:: with a different set of allocators from what stlport uses. 如果我没有注释掉该行,我试图添加的新库将无法编译,因为它使用std::与stlport使用的一组不同的分配器。 Type clashes. 键入冲突。

I've tried #define STLPORT std in a line just before including the new library, but this #define just doesn't seem to work. 我在包含新库之前尝试了#define STLPORT std ,但是这个#define似乎不起作用。

Is there a way to use the normal std once it gets redefined like this? 有没有一种方法可以使用正常的std一旦它被重新定义这样?

You can use something like: 您可以使用以下内容:

# if defined (_STLP_REDEFINE_STD)
#   undef  std
#   define std STLPORT
# else

//....

#ifdef std
#define std_WAS_DEFINED
#undef std
#endif

//new code

#ifdef std_WAS_DEFINED
#define std STLPORT
#endif

//legacy code

You have two different implementations of STL, one in the STLPORT and one in the standard library. 您有两种不同的STL实现,一种在STLPORT中,另一种在标准库中。 Your old code needs STLPORT and your new code needs the standard library. 您的旧代码需要STLPORT,您的新代码需要标准库。

Both new and old code use things like include <vector> but they need different include files named vector , and hence different -I compiler flags. 新旧代码都使用include <vector>类的东西,但是它们需要不同的包含vector文件,因此需要不同的-I编译器标志。 You cannot compile them with the same compiler settings. 您无法使用相同的编译器设置编译它们。

If you don't need to exchange standard containers and other STL-related data between old and new code, you can compile old code with the STLPORT settings and the new code with the regular settings. 如果您不需要在旧代码和新代码之间交换标准容器和其他STL相关数据,则可以使用STLPORT设置编译旧代码,使用常规设置编译新代码。 Do not touch STLPORT includes, do not redefine std in your code. 不要触摸STLPORT包含,不要在代码中重新定义std STLPORT is not meant to be used like that. STLPORT不应该像这样使用。 It is intended as a drop-in replacement for the (parts of) the standard library. 它旨在作为标准库(部分)的直接替代品。 You select which one you use by choosing the right compiler flags, not by modifying your (or STLPORT) source. 您可以通过选择正确的编译器标志来选择使用哪一个,而不是通过修改(或STLPORT)源。

If you do need to exchange standard containers between old and new code, you are out of luck. 如果你确实需要在旧代码和新代码之间交换标准容器,那你就不走运了。 Your only recourse is to modify the legacy code such that it conforms to the standard. 您唯一的办法是修改遗留代码,使其符合标准。

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

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