模板函数的模板参数
我刚刚发布了一个skiplist容器库。 Sun 编译器抱怨这一点:
template <class T, class R>
bool operator==(const IndexedSkipList<T,R> &left, const IndexedSkipList<T,R> &right)
{
return ((left.size() == right.size()) &&
(std::equal(left.begin(), left.end(), right.begin())));
}
错误是:
"include/CSIndexedSkipList.h", line 65: Error: Too few arguments for template std::reverse_iterator<CS::BidiIdxIterator<CS::IndexedSkipList<CS::T, CS::R>>>.
"include/CSIndexedSkipList.h", line 207: Where: While specializing "CS::IndexedSkipList<CS::T, CS::R>".
"include/CSIndexedSkipList.h", line 207: Where: Specialized in non-template code.
上面的代码是从 207 开始的。但它似乎抱怨反向迭代器。我真的无法理解它。我无法直接访问 Sun 编译器,所以我想知道我是否做错了什么。
另外,我在reverse_iterator中只使用了一个模板参数,但我注意到SGI文档说第二个参数T没有默认值。在我看过的所有地方,他们都只是使用这个:
typedef std::reverse_iterator<iterator> reverse_iterator;
That's line 65 that the compiler indicates关于。我需要添加 T 作为参数吗?我无法弄清楚有问题的错误。
顺便说一句,这适用于我能找到的所有平台上的 gcc。它在 Borland 中也能工作。
I just released a skiplist container library. And the Sun compiler complains about this:
template <class T, class R>
bool operator==(const IndexedSkipList<T,R> &left, const IndexedSkipList<T,R> &right)
{
return ((left.size() == right.size()) &&
(std::equal(left.begin(), left.end(), right.begin())));
}
The errors are:
"include/CSIndexedSkipList.h", line 65: Error: Too few arguments for template std::reverse_iterator<CS::BidiIdxIterator<CS::IndexedSkipList<CS::T, CS::R>>>.
"include/CSIndexedSkipList.h", line 207: Where: While specializing "CS::IndexedSkipList<CS::T, CS::R>".
"include/CSIndexedSkipList.h", line 207: Where: Specialized in non-template code.
The code above is what starts at 207. But it seems that it's complaining about the reverse_iterator. I can't really make sense of it. I don't have direct access to the Sun compiler, so I was wondering if I'm doing something wrong.
Also, I'm only using one template argument in reverse_iterator, but I noticed the SGI documentation saying that there is no default for the second argument T. Everywhere I've looked though, they just use this:
typedef std::reverse_iterator<iterator> reverse_iterator;
That's line 65 that the compiler complains about. Do I need to add T as a parameter? I can't figure out the error in question.
BTW, this works on gcc on all platforms I could find. And it works in Borland as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如比较 C++ 标准库 libCstd 和 libstlport 中所述,Sun C++ 编译器附带“C++ 标准库”的两个实现:libCstd 和 libstlport。不幸的是,libCstd 不符合标准,但出于向后兼容性的原因它是默认值。除其他差异外,libCstd 版本的
std::reverse_iterator
模板使用多个模板参数。您需要通过传入编译器选项
-library=stlport4
来指示编译器使用libstlport。另请参阅:
As explained at Comparing C++ Standard Libraries libCstd and libstlport, the Sun C++ compiler ships with two implementations of a "C++ standard library": libCstd and libstlport. Unfortunately, libCstd is not standards-conforming, but it is the default for backward-compatibility reasons. Among other differences, libCstd's version of the
std::reverse_iterator
template uses more than one template parameter.You need to instruct the compiler to use libstlport by passing in the compiler option
-library=stlport4
.See also:
顺便说一句,
-library=stlport4
不适用于在 Solaris 上运行的性能关键型多线程应用程序,因为 Sun Studio 12.1/12.2 附带的STLPort
版本要慢得多比 libCstd 慢,因为分配/释放时的自旋锁互斥体在 Solaris 上太慢。STLPort5
在这方面应该更好,但我未能在 Solaris 上构建它。至少可以说,Solaris 上似乎不再积极支持或使用STLPort
。因此,我们必须将所有软件(无论是在 SPARC 上还是在 x86 上)完全切换到 libCstd。By the way,
-library=stlport4
is NOT an option for performance-critical multithreaded applications running on Solaris because the version ofSTLPort
shipped with Sun Studio 12.1/12.2 is much slower thanlibCstd
due to spinlock mutexes on allocation/deallocation that are too slow on Solaris.STLPort5
should to be better in this regard, but I failed to build it on Solaris. It seems likeSTLPort
is no longer actively supported or used on Solaris, to say the least. So, we had to switch tolibCstd
completely for all of our software, both on SPARC and on x86.