【发布时间】:2020-08-16 19:57:37
【问题描述】:
我目前正在制作自定义 Vector 容器以用于学习。我的作业有限制,我应该使用 c++98 版本。
但是在执行“插入”功能的过程中,我遇到了我在this link 中解释的问题。
我得到了一些提示,我应该使用链接中的“enable_if”。但是因为我无权使用 c++11 (enable_if defined) 扩展,所以我自己做了 enable if 和 is_integral。
这是上一个。
// 1
template <typename T, typename Alloc>
void vector<T, Alloc>::insert(iterator position, size_type n, const value_type &val)
// 2
template <typename T, typename Alloc>
template <class InputIterator>
void vector<T, Alloc>::insert(iterator position, InputIterator first, InputIterator last)
这是我用自定义 enable_if 和 is_integral 更改的代码
// 1
template <typename T, typename Alloc>
void vector<T, Alloc>::insert(iterator position, size_type n, const value_type &val)
// 2
template <typename T, typename Alloc>
template <class InputIterator, class = typename ft::enable_if<!ft::is_integral<InputIterator>::value>::type>
void vector<T, Alloc>::insert(iterator position, InputIterator first, InputIterator last)
如果我不给编译器“-std=c++98”选项,它会很好地工作, 但是如果我给它,它会说
./Vector.hpp:87:33: warning: default template arguments for a function template are a C++11 extension [-Wc++11-extensions]
...class = typename ft::enable_if<!ft::is_integral<InputIterator>::value>::type>
^
您能帮我修改我的代码以适应 c++98 扩展吗?
【问题讨论】:
-
Use boost。它应该与 C++98 编译器一起工作,或者至少看看他们是如何实现这些模板的。
-
我也无权使用 boost。
-
哦,好主意。谢谢!
-
在你做实验的时候,老师并没有回头看你。首先使用
boost看看他们的enable_if和is_integral是否确实适用于您的C++98 项目。一旦你确认它可以工作,然后看看他们正在做什么来实现这些模板。 -
顺便说一句,实现这些模板的那些 boost 部分是仅头文件。这意味着您所要做的就是拥有适当的
#include文件以进行 boost 并查看您的代码是否可以编译(和工作)。