为什么在c++ 11或c++ 14中没有放置迭代器?

时间:2021-07-08 16:39:01

C++98 has front_inserter, back_inserter, and inserter, but there don't seem to be any emplacement versions of these in C++11 or draft C++14. Is there any technical reason we couldn't have front_emplacer, back_emplacer, and emplacer?

c++ 98有front_inserter、back_inserter和inserter,但是在c++ 11或草稿c++ 14中似乎没有任何这样的插入版本。有什么技术原因我们不能有front_emplacer, back_emplacer和emplacer吗?

1 个解决方案

#1


30  

Is there any technical reason we couldn't have front_emplacer, back_emplacer, and emplacer?

有什么技术原因我们不能有front_emplacer, back_emplacer和emplacer吗?

No, there is no technical reason. As proof here is a complete implementation of back_emplacer with a demo of your Use Case 1...

不,没有技术原因。作为证明,这里有一个完整的back_emplacer实现,其中演示了您的用例1……

#include <iterator>
#include <vector>
#include <iostream>

template<class Container>
class back_emplace_iterator : public std::iterator< std::output_iterator_tag,
                                                   void, void, void, void >
{
protected:
    Container* container;
public:
    typedef Container container_type;

    explicit back_emplace_iterator(Container& x) : container(&x) {}

    template<class T>
    back_emplace_iterator<Container>&
    operator=(T&& t)
    {
        container->emplace_back(std::forward<T>(t));
        return *this;
    }

    back_emplace_iterator& operator*() { return *this; }
    back_emplace_iterator& operator++() { return *this; }
    back_emplace_iterator& operator++(int) { return *this; }
};

template< class Container >
inline back_emplace_iterator<Container>
back_emplacer( Container& c )
{
    return back_emplace_iterator<Container>(c);
}

struct Demo
{
    int i;
    Demo(int i) : i(i) {}
};

int main()
{
    std::vector<int> x = {1,2,3,4,5};

    std::vector<Demo> y;

    std::copy(x.begin(), x.end(), back_emplacer(y));

    for (auto d : y)
        std::cout << d.i << std::endl;
}

Possible Known Issue: Does the universal reference of operator= hide an implicitly generated copy/move operator=? If so these need to be explicitly defined in a way that beats the universal reference in overload resolution.

可能已知的问题:操作符的通用引用是否隐藏了隐式生成的复制/移动操作符=?如果是这样,就需要显式地定义这些参数,使其在重载解析中优于通用引用。

#1


30  

Is there any technical reason we couldn't have front_emplacer, back_emplacer, and emplacer?

有什么技术原因我们不能有front_emplacer, back_emplacer和emplacer吗?

No, there is no technical reason. As proof here is a complete implementation of back_emplacer with a demo of your Use Case 1...

不,没有技术原因。作为证明,这里有一个完整的back_emplacer实现,其中演示了您的用例1……

#include <iterator>
#include <vector>
#include <iostream>

template<class Container>
class back_emplace_iterator : public std::iterator< std::output_iterator_tag,
                                                   void, void, void, void >
{
protected:
    Container* container;
public:
    typedef Container container_type;

    explicit back_emplace_iterator(Container& x) : container(&x) {}

    template<class T>
    back_emplace_iterator<Container>&
    operator=(T&& t)
    {
        container->emplace_back(std::forward<T>(t));
        return *this;
    }

    back_emplace_iterator& operator*() { return *this; }
    back_emplace_iterator& operator++() { return *this; }
    back_emplace_iterator& operator++(int) { return *this; }
};

template< class Container >
inline back_emplace_iterator<Container>
back_emplacer( Container& c )
{
    return back_emplace_iterator<Container>(c);
}

struct Demo
{
    int i;
    Demo(int i) : i(i) {}
};

int main()
{
    std::vector<int> x = {1,2,3,4,5};

    std::vector<Demo> y;

    std::copy(x.begin(), x.end(), back_emplacer(y));

    for (auto d : y)
        std::cout << d.i << std::endl;
}

Possible Known Issue: Does the universal reference of operator= hide an implicitly generated copy/move operator=? If so these need to be explicitly defined in a way that beats the universal reference in overload resolution.

可能已知的问题:操作符的通用引用是否隐藏了隐式生成的复制/移动操作符=?如果是这样,就需要显式地定义这些参数,使其在重载解析中优于通用引用。