有没有一种方法来测试C ++类是否有一个默认的构造函数(比编译器提供的类型特点等)? [英] Is there a way to test whether a C++ class has a default constructor (other than compiler-provided type traits)?

查看:173
本文介绍了有没有一种方法来测试C ++类是否有一个默认的构造函数(比编译器提供的类型特点等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

traits类可以定义检查一个C ++类有一个成员变量,函数或类型(见<一href=\"http://stackoverflow.com/questions/1005476/how-to-detect-whether-there-is-a-specific-member-variable-in-class/2694110#2694110\">here).

Traits classes can be defined to check if a C++ class has a member variable, function or a type (see here).

奇怪的是,<一个href=\"https://svn.boost.org/trac/boost/wiki/LibrariesUnderConstruction#Boost.ConceptTraits\">ConceptTraits不包括性状检查,如果一个C ++类定义默认构造函数或给予构造?

Curiously, the ConceptTraits do not include traits to check if a C++ class defines a default constructor or given constructor?

灿特征可以用来检查构造presence?
如果是的话,怎么样?
如果不是,为什么它是不可能的?

Can traits be used to check the constructor presence? If yes, how? If not, why it is not possible?

推荐答案

不好意思回答可能自己的问题。

Sorry for answering may own question.

谷歌搜索我发现,实际的原因,我们不能检查,如果一个类有构造函数或析构函数是,用来检测一个类有一个成员已知的技术是基于取件的地址。但是构造函数和析构函数没有名字,我们不能把他们的地址。

Googling I have found that the actual reason we can not check if a class has constructor or a destructors is that, the known technique used to detect if a class has a member is based on taking the address of the member. But constructors and destructors have no name, we can not take the address of them.

如果我们不能走地址,我没有看到一个方法,使编译器建设做出反应,而不直接实例化,但是在这种情况下,没有检测在编译时间,但一个错误。

If we can not take the address, I don't see a way to make the compiler react to a construction without instantiating it directly, but in this case there is no detection at compile time but an error.

因此​​,要回答我的问题,我会说,在目前的技术是不可能探测到它们,并需要编译器的支持。但C ++透露了不少惊喜,而且事情是不可能在特定的时间,被揭露有可能采用另一种技术。

So to answer my own question, I would say that with the current techniques it is not possible to detect them and compiler support is needed. But C++ has revealed a lot of surprises, and things that were not possible at a given time, were revealed are possible using another technique.

我希望一个C ++语言的专家都被读取并能给出更明确的解释。

I hope a C++ language expert is reading that and can give a more clear explanation.

这篇关于有没有一种方法来测试C ++类是否有一个默认的构造函数(比编译器提供的类型特点等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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