c – 从非成员模板函数访问私有内部类类型
发布时间:2020-12-16 07:10:56 所属栏目:百科 来源:网络整理
导读:请考虑以下代码: #include iostreamusing namespace std;class Outer { struct Inner { int num; };public: static Inner GetInner() { return Inner{-101};}};// void func1(Outer::Inner inner) { // [1] Does not compile as expected// cout inner.num
请考虑以下代码:
#include <iostream> using namespace std; class Outer { struct Inner { int num; }; public: static Inner GetInner() { return Inner{-101}; } }; // void func1(Outer::Inner inner) { // [1] Does not compile as expected // cout << inner.num <<endl; //} template <typename Dummy> void func2(Outer::Inner inner,Dummy = Dummy()) { cout << inner.num << endl; } int main() { // func1(Outer::GetInner()); // [2] does not compile as expected func2<int>(Outer::GetInner()); // [3] How does this compile? // Outer::Inner should not be accessible // from outside Outer return 0; } 我怎么能在非成员函数func2中使用类型为Outer :: Inner的参数,这是一个私有类型?当我尝试使用以下错误消息时,’func1正确地抱怨: prog.cpp: In function 'void func1(Outer::Inner)': prog.cpp:5:9: error: 'struct Outer::Inner' is private struct Inner { ^ prog.cpp:15:19: error: within this context void func1(Outer::Inner inner) { ^ 我在ubuntu上使用g 4.8.2,但我也在gcc-4.9.2上看到了这一点(在www.ideone.com上) 您可以在这里试用代码:Ideone 解决方法
我明白了
使用Visual Studio 2013更新4. Ergo,这是编译器中的一个问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |