Project

General

Profile

Actions

Feature #3093

closed

Backport make_unique

Added by Junxiao Shi over 8 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Category:
Base
Target version:
Start date:
Due date:
% Done:

100%

Estimated time:
2.00 h

Description

std::make_unique since C++14 is convenient for working with unique_ptrs.
We should backport it into ndn-cxx for use in C++11.

template<class T, class... Args>
unique_ptr<T>
make_unique(Args&&... args);

It's sufficient to backport only this overload for non-array type.

The overload for array types isn't used in ndn-cxx and is unnecessary.

This issue should change existing code to use make_unique.

All new and updated code should use make_unique after this issue closes.


Related issues 1 (0 open1 closed)

Related to NFD - Feature #3076: C++14 supportClosedDavide Pesavento

Actions
Actions #1

Updated by Junxiao Shi over 8 years ago

Question: should the backport be placed in std namespace or ndn namespace?

Actions #2

Updated by Junxiao Shi over 8 years ago

Actions #3

Updated by Davide Pesavento over 8 years ago

Junxiao Shi wrote:

This issue does not involve changing existing code to use make_unique.

Why not? It's trivial to port existing code over to make_unique, and not invasive, plus keeping a "mixed" codebase for a long time could be confusing to contributors.

Actions #4

Updated by Junxiao Shi over 8 years ago

Answer to note-3:

I don't know about an easy regex to exhaustively locate all violations in the codebase.
Without this regex, one cannot quickly determine whether a commit for this issue changes every possible place in the codebase to use make_unique.

However, an issue should have a clear boundary of completion, so I decide not to require such changes.
But I won't reject a patch that make one or more changes to use make_unique.

Actions #5

Updated by Davide Pesavento over 8 years ago

Something like 'unique_ptr.*new ' maybe (note the trailing whitespace)? It's not important if we do not catch all cases, the majority would be enough.

Actions #6

Updated by Junxiao Shi over 8 years ago

  • Description updated (diff)
  • Estimated time changed from 1.50 h to 2.00 h
Actions #7

Updated by Junxiao Shi over 8 years ago

20150901 conference call approves this feature.

Answer to note-1: This should be declared in std, and imported to ndn.

Actions #8

Updated by Junxiao Shi over 8 years ago

  • Assignee set to Anonymous

Specifically, this issue involves, at minimal:

  • in common.hpp, declare std::make_unique function template, whose semantic is equivalent to the same function template C++14
  • in common.hpp, add a using directive to import std::make_unique into ndn namespace
  • add a test suite for make_unique
Actions #9

Updated by Anonymous over 8 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 20
Actions #10

Updated by Davide Pesavento over 8 years ago

What do we do if we want to add make_unique to NFD?

  • We can't rely on the definition in ndn-cxx's common.hpp, because that file is considered private.
  • We probably can't define another make_unique in the std namespace, because it will conflict with the ndn-cxx definition, via indirect inclusion of common.hpp.
Actions #11

Updated by Davide Pesavento over 8 years ago

So I disagree with note 7. We should declare make_unique in namespace ndn only.

Actions #12

Updated by Davide Pesavento over 8 years ago

Moreover, adding new definitions to the std namespace is prohibited by the ISO C++ standard.

17.6.4.2.1 Namespace std

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a
namespace within namespace std unless otherwise specified. A program may add a template specialization
for any standard library template to namespace std only if the declaration depends on a user-defined type
and the specialization meets the standard library requirements for the original template and is not explicitly
prohibited.
Actions #13

Updated by Junxiao Shi over 8 years ago

  • Target version set to v0.4

I agree with note-11.

Actions #14

Updated by Davide Pesavento over 8 years ago

  • % Done changed from 20 to 70
Actions #15

Updated by Junxiao Shi over 8 years ago

Similar change should be applied to NFD as part of this issue. nfd::make_unique

It should be committed to master branch, and then feature-lp branch can be rebased to make use of it.

Actions #16

Updated by Junxiao Shi over 8 years ago

Having ndn::make_unique in common.hpp prevents it from being reused in other projects, because common.hpp is an implementation detail of ndn-cxx.

Other projects would have to make the same change, which causes code duplication and increases maintenance cost should a bug occur.

Recommendation:

  1. declare ndn::make_unique in util/backports.hpp

    Other backports, such as ndn::to_string, can be added to this file in the future.

  2. put #include "util/backports.hpp" at the end of common.hpp

    Usually common.hpp does not include other headers within ndn-cxx, but this one could be a special case.
    To avoid circular dependency, backports.hpp should not include common.hpp.

  3. test suite should be placed in backports.t.cpp

Actions #17

Updated by Davide Pesavento over 8 years ago

  • Assignee changed from Anonymous to Davide Pesavento

Ok, I'm taking over this task if Klaus doesn't mind.

Actions #18

Updated by Davide Pesavento over 8 years ago

  • Status changed from In Progress to Code review
  • % Done changed from 70 to 100
Actions #19

Updated by Davide Pesavento over 8 years ago

  • Status changed from Code review to Closed
Actions

Also available in: Atom PDF