STL source code analysis: bound friend template friend code testing and understanding

Purpose: Familiar with the source code of STL. I
recently read the book of STL source code analysis. I have read it before, but I didn’t experiment with these codes myself. So I may think I understand some concepts, but I don’t understand them at all. . For example, the following piece of code is excerpted from my own book. I use the ubuntu 18.04 version of the g++ compiler. It may be different for each compiler, but I just have a deeper understanding of the STL library and usage.
Among them are about the friend functions in the template class, and the functions of the function overloading of the friend functions. The code is directly given below, as well as the results of the test, and I will give my own insights later.

#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;
}

For the above code, the results of my test are as follows.
insert image description hereIt can be seen that the test is passed, but if the implementation of these functions is placed outside in the code I commented, it will not pass. This is inconsistent with the content in Hou Jie’s book, and it may be a problem with your own compiler, etc. . details as follows:

friend bool operator== <T> (const stack&, const stack&);

I always make mistakes in such a statement, and I checked the information later.
insert image description here
So try to use the traditional way to declare. At the same time, try to implement the code inside the template, such as the passed template given above.
This is the result of my own test. If there is any error, please discuss it together.

Comprehension:
In the template class, the friend function should be implemented in the class body as much as possible;

Guess you like

Origin blog.csdn.net/weixin_43851636/article/details/122061623