在C ++ 11和更高版本中的可选STL实现 [英] Alternative STL implementations in C++11 and beyond

查看:120
本文介绍了在C ++ 11和更高版本中的可选STL实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着时间的推移出现了STL *的各种替代实现 - 例如STLPort。某些大公司也使用自己的STL内部端口用于各种目的。



使用C ++ 03,可以使用便携式C ++编写STL端口



但是对于C ++ 11,不是有某些特性需要编译器

/ em>支持?



例如,我看不到 std :: is_standard_layout 只有C ++语言特性。我认为 std :: is_base_of 可以用 std :: is_convertible 来实现,使用基数和派生指针。但我不能想象如何可以实现 std :: is_standard_layout 。也可能有其他功能,以及没有发生在我身上。



那么,我在这里纠正吗?是不是不可能使用 C ++语言特性在C ++ 11中编写标准库的完整端口?



*我知道STL和C ++标准库不是严格可以互换的,但显然我的意思是这种情况下的C ++标准库。

解决方案

t在任何版本的C ++中编写一个完全可移植的标准C ++库实现!首先,一些标准的C ++库组件清楚地抽象系统细节。例如,文件流抽象对文件访问的访问。是的,你可以使用 FILE * ,但我认为标准C库是标准C ++库的一部分,可移植实现也需要包括该部分。此外,某些类型实际上取决于编译器,例如,因为存在与它们的语言级交互。例如,作为 dynamic_cast< ...>()的结果,抛出 std :: bad_cast 。另外,一些标准的C ++库组件需要使用关于内存布局的已知方法,并且 reinterpret_cast< ...>()执行正确的操作。在其他情况下,标准库指定不能确定的值,例如 std :: numeric_limits 的某些字段。



标准C ++库的总体思想是它涵盖了常见的需求,并实现了某些特性,这些特性不能被可移植和有效地实现。你引用的类型特征只是一些例子,编译器需要提供一些帮助。虽然我试图获得一些关于类型特征如何被编译器暴露的一致性,编译器编写者坚持认为他们需要在他们的选择上是免费的,标准的C ++库应该只给出特征暴露的方式的共同接口。


Over time various alternative implementations of the STL* have appeared - such as STLPort. Certain large corporations also use their own internal port of the STL for various purposes.

With C++03, it's possible to write a port of the STL using only portable C++ language features, meaning that any conforming compiler should be able to compile it.

But with C++11, aren't there certain features which require compiler support?

For example, I don't see how std::is_standard_layout could be implemented using only C++ language features. I think std::is_base_of could be implemented in terms of std::is_convertible, using base and derived pointers. But I can't imagine how std::is_standard_layout could be implemented. There may be other features as well which haven't occurred to me.

So, am I correct here? Is it impossible to write am complete port of the standard library in C++11 using only C++ language features?

*I know STL and "C++ standard library" are not strictly interchangeable, but obviously I mean the C++ Standard Libary in this case.

解决方案

You can't write a fully portable standard C++ library implementation in any version of C++! FIrst of all, some standard C++ library components clearly abstract the system specifics. For example, the file streams abstract the access to file access. Yes, you could use FILE* under the hood but I consider the standard C library to be part of the standard C++ library and a portable implementation would need to include that portion as well. Also, certain types are actually depending on the compiler, e.g., because there is language level interaction with them. For example std::bad_cast is thrown as a result of a dynamic_cast<...>(). Also, some standard C++ library components need to make use of knownledge about the memory layout and that reinterpret_cast<...>() does the Right Thing. In other cases, the standard library specifies values which cannot be determined portable, e.g., some of the fields of std::numeric_limits<T>.

The overall idea of the standard C++ library is that it covers common needs and implements certain features which can't be implemented portably and efficiently. The type traits you quoted are just some of the examples where compilers need to provide some help. Although I tried to get some agreement on how the type traits are exposed by the compilers, the compiler writers insisted that they need to be free on their choices and that the standard C++ library should just give a common interface to the way the traits are exposed.

这篇关于在C ++ 11和更高版本中的可选STL实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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