MyTinySTL icon indicating copy to clipboard operation
MyTinySTL copied to clipboard

关于测试mystl::vector<std::string> v3找不到可匹配的destroy的问题

Open wanfengcxz opened this issue 1 year ago • 1 comments

using std::string;
mystl::vector<string> v3;

如上代码所示,当定义v3变量时,编译器报错:

In template: call to function 'destroy' that is neither visible in the template definition nor found by argument-dependent lookup

即在如下位置: https://github.com/Alinshans/MyTinySTL/blob/acc07e025f521a28773ff82c4d11e249911ddcb2/MyTinySTL/construct.h#L62 提示找不到destroy

template <class ForwardIter>
void destroy_cat(ForwardIter first, ForwardIter last, std::false_type)
{
  for (; first != last; ++first)
    destroy(&*first);
}

此处应该是因为编译找不到destroy函数的声明,因为destroy定义在后面。所以,我认为应该在这个函数前面加上一个声明,即如下所示:

template<class Ty>
void destroy(Ty *pointer);

template <class ForwardIter>
void destroy_cat(ForwardIter first, ForwardIter last, std::false_type)
{
  for (; first != last; ++first)
    destroy(&*first);
}

加上之后,编译器就不再报错了,也能运行了~ 不知道这算不算一个bug。如果确实需要修复,能不能让我提个pr来修复一下(我会看一下pr规范的)

wanfengcxz avatar Aug 27 '23 07:08 wanfengcxz

我认为析构单个对象的函数应该叫 destroy_at (我们可以引入新函数,然后弃用旧的析构单对象版本)。 另外应该用类似 std::addressof 的机制避免 ADL 和重载 operator& 的问题。

frederick-vs-ja avatar Oct 09 '23 03:10 frederick-vs-ja