gpt4 book ai didi

c++ - SGI STL 中的绑定(bind)好友模板

转载 作者:行者123 更新时间:2023-11-30 05:18:27 25 4
gpt4 key购买 nike

我知道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==已声明为模板。

要解决此问题,请声明 operator==stack 的定义之前.但是如果你在那个时候声明它,你也可以定义它。并且声明将需要前向声明 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== 定义添加到顶部后,您将从底部删除它。)

关于c++ - SGI STL 中的绑定(bind)好友模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41743957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com