当前位置:网站首页>STL源码剖析:bound friend template friend代码测试和理解
STL源码剖析:bound friend template friend代码测试和理解
2022-07-30 05:50:00 【夕阳染色的坡道】
目的:熟悉STL的源码
最近看STL的源码剖析这本书,之前看过一遍,但是都是走马观花,没有自己亲自实验这些代码,所以对一些概念可能自己以为理解了,实际上根本没有了解。如下面自己的在书本上摘录的一段代码,我用的是ubuntu 18.04版本的g++编译器,可能每个编译器不一样,但是我只是深入了解一下,关于STL的库,以及用法。
其中的关于模板类中的友元函数,以及友元函数的函数重载的函数。下面直接给代码,以及测试的结果,后面将给予自己的感悟。
#include<iostream>
#include<cstddef>
class alloc
{
};
template <class T, class Alloc=alloc, size_t BufSiz=0>
class deque
{
public:
deque(){
std::cout <<"deque"<<' ';}
};
template <class T, class Sequence>
class stack;
template <class T, class Sequence>
bool operator == (const stack<T, Sequence>& x, const stack<T, Sequence>& y);
template <class T, class Sequence>
bool operator < (const stack<T, Sequence>& x, const stack<T, Sequence>& y);
template <class T, class Sequence = deque<T>>
class stack
{
friend bool operator == (const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
std::cout << "operator == " << '\t';
return true;
}
friend bool operator < (const stack<T, Sequence>& x, const stack<T, Sequence>& y)
{
std::cout << "operator < " << '\t';
return true;
}
//friend bool operator== <T> (const stack&, const stack&);
//friend bool operator< <T> (const stack&, const stack&);
//friend bool operator== < >(const stack&, const stack&);
//friend bool operator< < >(const stack&, const stack&);
public:
stack(){
std::cout << "stack" <<std::endl;}
private:
Sequence c;
};
/* template <class T, class Sequence> bool operator == (const stack<T, Sequence>& x, const stack<T, Sequence>& y) { return std::cout <<"operator == " << '\t'; } template <class T, class Sequence> bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>& y) { return std::cout<<"operator < " << '\t'; } */
int main()
{
stack<int> x;
stack<int> y;
std::cout << (x==y) << std::endl;
std::cout << (x<y) <<std::endl;
}
对于上述的代码,我测试的结果是如下。
可以看到通过测试,但是如果在我注释的代码中,将这些函数的实现放在外面的情况下,它是不通过的,这个和侯杰书中的内容不一致,也有可能是自己编译器问题等等。具体如下:
friend bool operator== <T> (const stack&, const stack&);
自己这样申明总是出错,后面查阅了资料。
所以尽量使用传统的那种方式申明。同时尽量在模板内部实现代码,如上面给予的通过的模板。
这是自己测试的结果,如果有错误欢迎大家一起讨论。
感悟:
在模板类中,友元函数尽量在类体进行实现;
边栏推荐
- 一种分布式深度学习编程新范式:Global Tensor
- 如何将modelsim仿真数据存成文件
- The Force Plan Microservices | Centralized Configuration Center Config Asymmetric Encryption and Security Management
- GAIA-IR:GraphScope 上的并行化图查询引擎
- 事件传递和响应者链条
- GNNLab:基于空间共享思想设计的新型 GNN 系统
- DNS域名解析服务
- As a test leader, examine several aspects of job candidates
- Test Development Engineer Growth Diary 008 - Talking About Some Bugs/Use Case Management Platform/Collaboration Platform
- 远程连接服务器的MySql
猜你喜欢

掌握JESD204B(二)–AD6676的调试

如何使用xilinx的FFT ip

测试开发工程师成长日记016 - 关于提测的那些事

Test Development Engineer Growth Diary 003 - Interface Automation Framework Construction

Ingress:从静态图分析到动态图分析

测试开发工程师成长日记017 - bug的生命周期

npm安装nodejs环境配置

About memcache kernel, so one of the most popular

Build an intelligent network security management and control system for digital government

How to import matlab data into modelsim simulation
随机推荐
Rapidly develop GraphScope graph analysis applications
How to import matlab data into modelsim simulation
基于 JupyterLab 插件在 GraphScope 中交互式构图
掌握JESD204B(二)–AD6676的调试
Test development engineer growth diary 016 - those things about the test
测试开发工程师成长日记003 - 接口自动化框架搭建
如何使用xilinx的FFT ip
测试开发工程师成长日记008 - 浅谈一些Bug/用例管理平台/协作平台
快速开发 GraphScope 图分析应用
MongoDB - query
测试开发工程师成长日记015 - 最强20道测试面试题
事件传递和响应者链条
图计算在网络安全分析中的应用
Multithreading basics (multithreaded memory, security, communication, thread pools and blocking queues)
测试开发工程师成长日记010 - Jenkins中的CI/CD/CT(持续集成构建/持续交付/持续测试)
Dachang's annual salary of 50w+ recruits test engineers with test platform development capabilities
Swagger使用方式,告别postman
prometheus-tls加密
GadgetInspector principle analysis
GAIA-IR: Parallelized Graph Query Engine on GraphScope