如何在C ++内部实现显式 [英] How explicit is implemented internally in C++

查看:107
本文介绍了如何在C ++内部实现显式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在C ++中,动态多态(虚拟)由vptr和vtables实现.在同一行中,我可以知道如何实现explicit关键字(用于构造函数和复制构造函数)吗?

Yuvaraj

Hi,

In C++ dynamic polymorphism (virtual) is implemented by vptr and vtables. In the same line, can I know how explicit keyword (used for constructor and copy constructor) is implemented?

Yuvaraj

推荐答案

不需要真正的运行时实现.可以完成此工作的是编译器:如果将构造函数标记为显式,则编译器根本不允许隐式转换.遇到这种情况时,编译器会为您执行隐式转换.因此,如果构造函数被标记为显式的,它也完全不会执行该转换.
There''s no real runtime implementation needed. It''s the compiler which can do the job: if a constructor has been marked as explicit, the compiler simply doesn''t allow implicit conversions. The compiler does an implicit conversion for you when such a case is encountered. So, it can also perfectly not do that conversion if the constructor is marked as explicit.


explicit 只是告诉编译器不要使用该构造函数来转换隐式转换" .

如果需要隐式转换,则编译器将简单地寻找另一个合适的函数(例如Class::operator Type())

函数本身不是不同的".在翻译后的代码中没有那么有效的内容.

请注意,explicit关键字只有在构造函数中才有线索,因为只能在其中使用一个参数,因为在转换中使用的是该参数.
explicit just tells the compiler to "don''t use that constructor to translate implicit conversion".

In case an implicit conversion is required, the compiler will simply look for another suitable function (like a Class::operator Type())

The function itself is not "different". no so effert will go in the translated code.

Note that the explicit keyword has a clue only in constructor that can take only one argument, since are the ones used in conversions.


它在编译器前端的某个位置实现,不会在代码生成器附近找到任何地方,而且我相当确定它不会在重载解析中发挥任何作用.

通过在两个不同的翻译单元中定义一个类的两个不同版本,您可以看到这一点,唯一的区别是使用显式.所以在其中:

It''s implemented somewhere in the compilers front end, it doesn''t get anywhere near the code generator and I''m fairly sure it doesn''t play any part in overload resolution.

You can see this by defining two different versions of a class in two different translation units, the only difference being the use of explicit. So in one:

class A
{
    public:
        explicit A();
};



另一个:



and in the other:

class A
{
    public:
        A();
};



并在各自的翻译单元中使用该类的两个定义.

然后,您可以在第三个翻译单元中实现这两个版本:



and use both definitions of the class in their respective translation units.

You can then implement both versions in a third translation unit:

class A
{
    public:
        A();
};

A::A()
{
}


嘿,在其他两个翻译单元中的任何一个中,该实现将同时满足显式和非显式使用.顺便说一下,您可以对public,protected和private进行相同的操作.

那么这是什么意思?据我所知,这意味着没有任何关于构造函数是否被标记为显式的信息最终会随其实现一起出现在目标文件中.

不过我可能是错的,您的编译器可能会有所不同.不要在生产代码中依赖它.

干杯,


and hey presto the implementation will satisfy both explicit and non-explicit uses in either of the other two translation units. Incidentally you can do the same trick with public, protected and private.

So what does this mean? As far as I can tell it means no information about whether a constructor is marked explicit ends up in the object file along with it''s implementation.

I may be wrong though, your compiler may vary. Don''t rely on it in production code.

Cheers,

Ash


这篇关于如何在C ++内部实现显式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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