Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:52:09+00:00 2026-06-10T09:52:09+00:00

I have used vector::emplace_back in order to avoid constructing temporal objects while filling a

  • 0

I have used vector::emplace_back in order to avoid constructing temporal objects while filling a vector. Here you have a simplified version:

class Foo {
public:
    Foo(int i, double d) : i_(i), d_(d) {}
    /* ... */
};

std::vector<Foo> v;
v.reserve(10);
for (int i = 0; i < 10; i++)
    v.emplace_back(1, 1.0);

But I wanted to use std::fill_n instead:

v.reserve(10);
std::fill_n(std::back_inserter(v), 10, Foo(1, 1.0));

In this way, temporal copies will be created, though. I do not know how to use emplace in this situation. I guess I would need something like std::back_emplacer, but I could not find such a thing. Is that part of C++11, but not implemented in GCC yet? If it is not part of C++11, is there any other way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:52:11+00:00Added an answer on June 10, 2026 at 9:52 am

    It’s common to use tuples to ease the pass a variadic number of items (in this case, parameters to forward to emplace_back), with a little technique to unpack the tuple back. As such it is possible to write a back_emplacer utility by requiring the user to make use of the tuple factory functions (one of std::make_tuple, std::tie, std::forward_as_tuple) where it make sense:

    #include <type_traits>
    #include <tuple>
    
    // Reusable utilites
    
    template<typename T>
    using RemoveReference = typename std::remove_reference<T>::type;
    template<typename T>
    using Bare = typename std::remove_cv<RemoveReference<T>>::type;
    
    template<typename Out, typename In>
    using WithValueCategoryOf = typename std::conditional<
        std::is_lvalue_reference<In>::value
        ,  typename std::add_lvalue_reference<Out>::type
        , typename std::conditional<
            std::is_rvalue_reference<Out>::value
            , typename std::add_rvalue_reference<Out>::type
            , Out
        >::type
    >::type;
    
    template<int N, typename Tuple>
    using TupleElement = WithValueCategoryOf<
        typename std::tuple_element<N, RemoveReference<Tuple>>::type
        , Tuple
    >;  
    
    // Utilities to unpack a tuple
    template<int... N>
    struct indices {
        using next = indices<N..., sizeof...(N)>;
    };
    
    template<int N>
    struct build_indices {
        using type = typename build_indices<N - 1>::type::next;
    };
    template<>
    struct build_indices<0> {
        using type = indices<>;
    };
    
    template<typename Tuple>
    constexpr
    typename build_indices<std::tuple_size<Bare<Tuple>>::value>::type
    make_indices() { return {}; }
    
    template<typename Container>
    class back_emplace_iterator {
    public:
        explicit back_emplace_iterator(Container& container)
            : container(&container)
        {}  
    
        template<
            typename Tuple
            // It's important that a member like operator= be constrained
            // in this case the constraint is delegated to emplace,
            // where it can more easily be expressed (by expanding the tuple)   
            , typename = decltype( emplace(std::declval<Tuple>(), make_indices<Tuple>()) )
        >
        back_emplace_iterator& operator=(Tuple&& tuple)
        {
            emplace(*container, std::forward<Tuple>(tuple), make_indices<Tuple>());
    
            return *this;
        }
    
        template<
            typename Tuple
            , int... Indices  
            , typename std::enable_if<
                std::is_constructible<
                    typename Container::value_type
                    , TupleElement<Indices, Tuple>...
                >::value
                , int
            >::type...
        >
        void emplace(Tuple&& tuple, indices<Indices...>)
        {
            using std::get;
            container->emplace_back(get<Indices>(std::forward<Tuple>(tuple))...);
        }
    
        // Mimic interface of std::back_insert_iterator
        back_emplace_iterator& operator*() { return *this; }
        back_emplace_iterator& operator++() { return *this; }
        back_emplace_iterator operator++(int) { return *this; }
    
    private:
        Container* container;  
    };
    
    template<typename Container>
    back_emplace_iterator<Container> back_emplacer(Container& c)
    { return back_emplace_iterator<Container> { c }; }
    

    A demonstration of the code is available. In your case you’d want to call std::fill_n(back_emplacer(v), 10, std::forward_as_tuple(1, 1.0)); (std::make_tuple is also acceptable). You’d also want the usual iterator stuff to make the feature complete — I recommend Boost.Iterators for that.

    I must really stress however that such a utility doesn’t bring much when used with std::fill_n. In your case it would save the construction of the temporary Foo, in favour of a tuple of references (a tuple of values if you were to use std::make_tuple). I leave it to the reader to find some other algorithm where back_emplacer would be useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before C++11 I have used swap-to-back to avoid deep copy overheads, like: vector<vector<Object> >
What I have: Vectors of different custom structs(one custom struct per vector) I used
I have used geocoder in my ruby on rails application for a while but
I have a vector class with hashCode() implemented. It wasn't written by me, but
Looking at vector , I realized that I have never used the second argument
It has been a while since I have used templates with C++, but now
Imagine I have a class used to represent some trivial numerical data, like a
I have used the silverlight control in CRM 2011.It also published on form but
We have used Shibboleth to authenticate users. It works great. The issue is that
I have used some jquery components in my web site, Suddenly i'm getting an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.