6

Suppose we want design a container C similar to std::vector. Is it a good idea to implement push_back by calling emplace_back, such as:

template <typename T>
class C {
  public:
    ...
    template <typename Args...>
    void emplace_back(Args&&... args) { 
      ... // uses T(std::forward<Args>(args)...) internally
    }
    void push_back(T value) {
      emplace_back(std::move(value));
    }
    ...
};

or, alternatively:

    template <typename U>
    void push_back(U&& value) {
      emplace_back(std::forward(value));
    }

?

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • This type of question may get a better response from https://codereview.stackexchange.com/. – François Andrieux Aug 03 '17 at 18:56
  • 3
    This is exactly how it is implemented for vector in msvc (const& and && overloads). – Sopel Aug 03 '17 at 18:58
  • @FrançoisAndrieux Thanks for pointing this out, haven't known about this site. Is it commonly used? There is only 4,917 C++ tagged questions on CR, in contrast to 526,886 on SO. – Daniel Langr Aug 03 '17 at 19:06
  • @Sopel What version are you referring to? That is not correct for VS2015 (14.0). – François Andrieux Aug 03 '17 at 19:09
  • @FrançoisAndrieux i'm talking about version 14.1 +, which is shipped since vs2017 – Sopel Aug 03 '17 at 19:11
  • 1
    @DanielLangr It's less active and younger than Stack Overflow but you can get better results because the question would better target it's user base. – François Andrieux Aug 03 '17 at 19:11
  • 2
    @DanielLangr Yes, codereview is commonly used. Do be aware that the expected question format is very different from on Stack Overflow, so you'll want to check the help center and maybe try to get guidance from their chat. – Justin Aug 03 '17 at 19:12

0 Answers0