SGI STL 中的绑定好友模板
bound friend templates in SGI STL
我知道SGI STL和我一样古老,但我仍然想弄清楚。
在 stl_stack.h 中,有一些代码如下:
template <class T, class Sequence = Deque<T> >
class Stack {
friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
friend bool operator< __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
protected:
Sequence c;
};
template <class T, class Sequence>
bool operator== (const Stack<T, Sequence>& x, const Stack<T, Sequence>& y){
return x.c == y.c;
}
template <class T, class Sequence>
bool operator< (const Stack<T, Sequence>& x, const Stack<T, Sequence>& y){
return x.c < y.c;
}
在 stl_config.h 中,__STL_NULL_TMPL_ARGS
定义如下:
# ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
# define __STL_TEMPLATE_NULL template<>
# else
# define __STL_TEMPLATE_NULL
# endif
但是当我尝试用G ++ 4.9.2编译它时,编译器是这样说的:
In file included from stack.cpp:1:0:
stack.h:13:22: error: declaration of ‘operator==’ as non-function
friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
^
stack.h:13:22: error: expected ‘;’ at end of member declaration
In file included from iterator.h:3:0,
from deque.h:6,
from stack.h:5,
from stack.cpp:1:
stl_config.h:111:31: error: expected unqualified-id before ‘<’ token
# define __STL_NULL_TMPL_ARGS <>
^
stack.h:13:25: note: in expansion of macro ‘__STL_NULL_TMPL_ARGS’
friend bool operator== __STL_NULL_TMPL_ARGS (const Stack&, const Stack&);
我不知道为什么完全相同的代码不能在我的电脑上编译,这个代码现在是非法代码还是什么?
谢谢!!!
stl_stack.h
有一个错误,这个错误一定是当代编译器所关注的。在operator== <>
被声明为模板之前提及operator==
是违法的。
要解决此问题,请在定义stack
之前声明operator==
。但是,如果你在这一点上声明它,你不妨定义它。声明将要求向前声明stack
:
template <class T, class Sequence>
class stack; // Forward declare for sake of operator== declaration.
template <class T, class Sequence>
bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {
return x.c == y.c;
}
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >
#else
…
(当然,将operator==
定义添加到顶部后,您将从底部删除它。
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 在基于范围的for循环中使用结构化绑定声明
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 视觉studo 2019中的漫画和静态/动态绑定
- 将自由函数绑定为类成员函数
- 将常量指针引用绑定到非常量指针
- 在派生类中绑定非静态模板化成员函数
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 在 openGL 中多次绑定缓冲区
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 使用结构化绑定'Reflection'
- 为什么 std::绑定错误参数可以成功?
- 如何绑定 C++ gRPC 客户端的网络接口
- 在 openmp 中,omp_get_thread_num是否绑定到物理线程?
- C++绑定(已弃用)
- 运行时错误:引用绑定到类型为"int"的空指针
- 有没有办法将重载的类函数绑定到函数对象?
- SGI STL 中的绑定好友模板