2

我构建了一个简单的类,它应该模仿 std::string 类的功能(作为练习!):

#ifndef _STR12_1_H
#define _STR12_1_H

#include <string>
#include <iostream>

class Str12_1
{
public:

    typedef char* iterator;
    typedef const char* const_iterator;
    typedef long size_type;


    Str12_1();
    Str12_1(const Str12_1& str);
    Str12_1(const char *p);
    Str12_1(const std::string& s);

    size_type size() const;

    //Other member functions


private:
    iterator first;
    iterator onePastLast;
    iterator onePastAllocated;
};

为了避免与“new”相关的开销(并增加我对<memory>标题的熟悉度),我选择使用库的分配器模板类来为我的字符串分配内存。这是我在复制构造函数中使用它的示例:

#include <memory>
#include <algorithm>

using std::allocator;
using std::raw_storage_iterator;
using std::uninitialized_copy;


Str12_1::Str12_1(const Str12_1& str)
{
    allocator<char> charAlloc;
    first = charAlloc.allocate(str.size());
    onePastLast = onePastAllocated = first + str.size();
    *onePastLast = '\0';

    raw_storage_iterator<char*, char> it(first);

    uninitialized_copy(str.first, str.onePastLast, it);


}

编译器不断告诉我“uninitialized_copy”行上的两个错误,这两个错误都导致库中的标题,:

error: invalid conversion from 'char' to 'char*'

error: no match for 'operator!=' in '__first != __last'

问题是我不明白从 char 到 char* 的转换在哪一行,以及为什么不能将相同类型的两个指针(str.first、str.onePastLast)与“!=”进行比较。

我可以使用“新”,但如前所述,我想练习<memory>. 那么有人可以告诉我为什么这不起作用吗?

4

1 回答 1

5

查看标准raw_storage_iterator不会 typedefvalue_typeTvoid而是:

template <class OutputIterator, class T>
class raw_storage_iterator
: public iterator<output_iterator_tag,void,void,void,void>
                                      ^^^^

uninitialized_copy必须使用该typedef:

template <class InputIterator, class ForwardIterator>
ForwardIterator uninitialized_copy(InputIterator first, InputIterator last,
ForwardIterator result);

效果:

for (; first != last; ++result, ++first)
::new (static_cast<void*>(&*result))
typename iterator_traits<ForwardIterator>::value_type(*first);
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

在您的代码中,在所有替换之后,这会导致:

new (...&*result) void (*first);
                  ^^^^^^^^^^^^^
                 invalid use here

从那你可以得出结论,这两个从来没有打算一起工作。

如果你想使用raw_storage_iterator,那么将它传递给应该没问题,std::copy因为所有的魔法都发生在operator=(const T&)重载中。

如果你认为这对于一个原语来说是必要的,比如char你可以用new char[x](NB! terminating NUL) 分配并用strcpy.

于 2011-02-01T23:02:19.407 回答