惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
T
The Blog of Author Tim Ferriss
H
Help Net Security
博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
博客园 - 聂微东
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
B
Blog
Engineering at Meta
Engineering at Meta
V
V2EX
Hugging Face - Blog
Hugging Face - Blog

博客园 - 吴尔平

gtest 的彩色信息输出 + boost.test 的内存泄漏检测及定位 在低版本的 vc 中使用 vc 10.0 的新特性 使用另一个blog: http://blog.csdn.net/WuErPing scons + swig 如何在 vista 使用 Device Emulator 连接internet vc9 Feature Pack Beta tr1 的一些问题 NSIS Kill Process (转贴) C#与一个彩票页面 - 吴尔平 - 博客园 py2exe 转换 pytetris - 吴尔平 C# 的 random shuffle python 读取 windows event log 的简短代码 IronPython 1.0 Release Candidate (转贴 ( 据说比c实现的快1.5!) ) Visual Studio Service Pack (转贴) The 16th Annual Jolt Product Excellence Award Winners (转贴) C++/CLI FAQ (逐步整理中) C++/CLI singleton模式 (双重检测锁实现) 如何在 VS2005 的 Team Unit Testing frameworks 中测试 Native Code (C++ ) 2005 CRT memory leaks 改变 SQL Server 2000 所有对象的所有者
关于模板化的friend class
吴尔平 · 2007-03-17 · via 博客园 - 吴尔平

    2007-3-17 10:42   By WuErPing

    首先,C++标准在这个地方定义得不精确,这就给厂商的实现带来了想象的空间。VC和GCC在这里就有不得不注意的差别
    代码一、VC的实现(7.1, 8)

 1         #include <iostream>
 2         using namespace std;
 3         
 4         #include <iostream>
 5         using namespace std;
 6         
 7         template<typename _T>
 8         class MyClass
 9         {
10         public:
11             friend _T;
12         private:
13             void PrivatePrint(){ cout << "void PrivatePrint()" << endl;};
14         };
15         
16         class MyFriend
17         {
18         public:
19             template<typename _T>
20             void print(MyClass<_T>& my)
21             {
22                 my.PrivatePrint();
23             }
24         };
25         
26         int _tmain(int argc, _TCHAR* argv[])
27         {
28             MyClass<MyFriend> my;
29             MyFriend myf;
30             myf.print(my);
31             return 0;
32         }

    代码二、GCC(3.4.4)

 1         #include <iostream>
 2         using namespace std;
 3         template <class _T>
 4         class ExtractType
 5         {
 6         public:
 7             typedef _T type;
 8         };
 9         
10         template<typename _T>
11         class MyClass
12         {
13         public:
14             friend class ExtractType<_T>::type;
15         private:
16             void PrivatePrint(){ cout << "void PrivatePrint()" << endl;};
17         };
18         
19         class MyFriend
20         {
21         public:
22             template<typename _T>
23             void print(MyClass<_T>& my)
24             {
25                 my.PrivatePrint();
26             }
27         };
28         
29         int main(int argc, char* argv[])
30         {
31             MyClass<MyFriend> my;
32             MyFriend myf;
33             myf.print(my);
34             return 0;
35         }

比较两个代码的实现,VC的方法显得自然而易于理解,也符合常理,要用到技巧来解决语法问题终归是对程序员的不友好 。 不过没有使用4.0以上的GCC版本,不知道是否有改变。