c – 为什么我可以在decltype()中使用私有默认构造函数?

c – 为什么我可以在decltype()中使用私有默认构造函数?,第1张

概述看看代码: #include <iostream>#include <utility>class test{private: test() { }public: test foo() { return *this; } static const char *name() { return "test"; }};int main(){ std:: 看看代码:
#include <iostream>#include <utility>class test{private:    test() { }public:    test foo() { return *this; }    static const char *name() { return "test"; }};int main(){    std::cout << decltype(test().foo())::name() << std::endl;               // 1    std::cout << decltype(std::declval<test>().foo())::name() << std::endl; // 2}

我期望// 1行无法编译,因为test的默认构造函数是private.

However,it works well.我在我的g 4.8.3上测试了它–Wall -Wextra -Werror -pedantic难以置信,但它运行良好,没有任何错误或警告.

(此外,它似乎也适用于GCC 4.9.1.)

从this page开始,如果表达式未被评估,我想我们可以使用私有默认构造函数.所以,我测试了以下内容来检查它.

#include <iostream>#include <utility>class test{private:    test(int) { }public:    test foo() { return *this; }    static const char *name() { return "test"; }};int main(){    std::cout << decltype(test().foo())::name() << std::endl;               // 1    std::cout << decltype(std::declval<test>().foo())::name() << std::endl; // 2}

(live example)

正如所料,它没有编译.

但为什么??怎么可能?我们可以在未评估的表达中使用私人成员吗?或者默认构造函数是否有特殊规则?你能解释一下为什么吗?

解决方法 它不应该编译. C 11 [class.temporary]有关于创建临时对象的说法:

12.2/1 Even when the creation of the temporary object is unevaluated
or otherwise avoIDed,all the semantic restrictions shall be respected as if the temporary object had been created and later destroyed. [ Note: even if there is no call to the destructor or copy/move constructor,all the semantic restrictions,such as accessibility and whether the function is deleted,shall be satisfIEd. However,in the special case of a function call used as the operand of a decltype-specifIEr,no temporary is introduced,so the foregoing does not apply to the prvalue of any such function call. — end note ]

因此,即使未经评估,您仍然受限于创建和销毁临时所需的任何函数(包括构造函数)的可访问性.该注释的最后一句澄清了像declval这样的函数可以用来避免这个障碍.

总结

以上是内存溢出为你收集整理的c – 为什么我可以在decltype()中使用私有默认构造函数?全部内容,希望文章能够帮你解决c – 为什么我可以在decltype()中使用私有默认构造函数?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1241263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存