哪些标准的c ++类不能在c ++中重新实现? [英] Which standard c++ classes cannot be reimplemented in c++?

查看:183
本文介绍了哪些标准的c ++类不能在c ++中重新实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览C ++ 0x的计划,并在 std :: initializer_list 中实现用户类中的初始化列表。这个类不能在C ++
中实现,而不使用自身,或者使用一些编译器魔法。如果可以,就不需要它,因为你用来实现 initializer_list 的任何技术都可以用来在你自己的类中实现初始化列表。

I was looking through the plans for C++0x and came upon std::initializer_list for implementing initializer lists in user classes. This class could not be implemented in C++ without using itself, or else using some "compiler magic". If it could, it wouldn't be needed since whatever technique you used to implement initializer_list could be used to implement initializer lists in your own class.

什么其他类需要某种形式的编译器魔法工作?哪些类在标准库中,不能由第三方库实现?

What other classes require some form of "compiler magic" to work? Which classes are in the Standard Library that could not be implemented by a third-party library?

编辑:也许不是实现,我应该实例化。更多的事实是,这个类是直接链接到一个语言功能(你不能使用初始化列表没有 initializer_list )。

Maybe instead of implemented, I should say instantiated. It's more the fact that this class is so directly linked with a language feature (you can't use initializer lists without initializer_list).

与C#的比较可能会清除我想知道的内容:IEnumerable和IDisposable实际上是硬编码为语言特性。我一直认为C ++是免费的,因为Stroustrup试图使一切都可以在库中实现。

A comparison with C# might clear up what I'm wondering about: IEnumerable and IDisposable are actually hard-coded into language features. I had always assumed C++ was free of this, since Stroustrup tried to make everything implementable in libraries. So, are there any other classes / types that are inextricably bound to a language feature.

推荐答案

std :: type_info 是一个简单的类,虽然填充它需要 typeinfo :编译器构造。

std::type_info is a simple class, although populating it requires typeinfo: a compiler construct.

同样,异常是正常的对象,但是抛出异常需要编译器的魔法(分配异常在哪里)。

Likewise, exceptions are normal objects, but throwing exceptions requires compiler magic (where are the exceptions allocated?).

问题是,我们可以得到 std :: initializer_list 没有编译器魔术?

The question, to me, is "how close can we get to std::initializer_lists without compiler magic?"

查看 wikipedia std :: initializer_list< typename T> 可以被看起来很像数组字面量的东西初始化。让我们尝试给我们的 std :: initializer_list< typename T> 一个转换构造函数,它接受一个数组(即,一个构造函数接受 T [] ):

Looking at wikipedia, std::initializer_list<typename T> can be initialized by something that looks a lot like an array literal. Let's try giving our std::initializer_list<typename T> a conversion constructor that takes an array (i.e., a constructor that takes a single argument of T[]):

namespace std {
     template<typename T> class initializer_list {
         T internal_array[];
         public:
         initializer_list(T other_array[]) : internal_array(other_array) { };

         // ... other methods needed to actually access internal_array
     }
}

$ b类似地,使用 std :: initializer_list 的类通过声明一个构造函数来获取一个 std :: initializer_list 参数 - 也称为转换构造函数:

Likewise, a class that uses a std::initializer_list does so by declaring a constructor that takes a single std::initializer_list argument -- a.k.a. a conversion constructor:

struct my_class {
    ...
    my_class(std::initializer_list<int>) ...
}

所以行:

 my_class m = {1, 2, 3};

导致编译器认为:我需要调用 my_class ; ; my_class 有一个构造函数,它接受一个 std :: initializer_list< int> int [] 字面值;我可以将 int [] 转换为 std: :initializer_list< int> ;我可以将它传递给 my_class 构造函数C ++不允许两个隐式的用户定义的转换链接)。

Causes the compiler to think: "I need to call a constructor for my_class; my_class has a constructor that takes a std::initializer_list<int>; I have an int[] literal; I can convert an int[] to a std::initializer_list<int>; and I can pass that to the my_class constructor" (please read to the end of the answer before telling me that C++ doesn't allow two implicit user-defined conversions to be chained).

那么这是多近?首先,我缺少初始化列表的一些功能/限制。我不强制的一个事情是初始化列表只能用数组文字构造,而我的 initializer_list 也将接受一个已经创建的数组:

So how close is this? First, I'm missing a few features/restrictions of initializer lists. One thing I don't enforce is that initializer lists can only be constructed with array literals, while my initializer_list would also accept an already-created array:

int arry[] = {1, 2, 3};
my_class = arry;

此外,我没有打扰讨论右值引用。

Additionally, I didn't bother messing with rvalue references.

最后,这个类只有当新的标准说,如果编译器隐式链接两个用户定义的转换一起工作。这是在正常情况下特别禁止的,所以示例仍然需要编译器魔术。但我认为(1)类本身是一个普通类,(2)涉及的魔法(强制执行数组文字初始化语法,允许两个用户定义的转换被隐式链接)小于第一眼。

Finally, this class only works as the new standard says it should if the compiler implicitly chains two user-defined conversions together. This is specifically prohibited under normal cases, so the example still needs compiler magic. But I would argue that (1) the class itself is a normal class, and (2) the magic involved (enforcing the "array literal" initialization syntax and allowing two user-defined conversions to be implicitly chained) is less than it seems at first glance.

这篇关于哪些标准的c ++类不能在c ++中重新实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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