Feature #3093
closedBackport make_unique
Added by Junxiao Shi over 9 years ago. Updated about 9 years ago.
100%
Description
std::make_unique
since C++14 is convenient for working with unique_ptr
s.
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.
Updated by Junxiao Shi over 9 years ago
Question: should the backport be placed in std
namespace or ndn
namespace?
Updated by Junxiao Shi over 9 years ago
- Related to Feature #3076: C++14 support added
Updated by Davide Pesavento over 9 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.
Updated by Junxiao Shi over 9 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
.
Updated by Davide Pesavento over 9 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.
Updated by Junxiao Shi over 9 years ago
- Description updated (diff)
- Estimated time changed from 1.50 h to 2.00 h
Updated by Junxiao Shi about 9 years ago
20150901 conference call approves this feature.
Answer to note-1: This should be declared in std
, and imported to ndn
.
Updated by Junxiao Shi about 9 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 importstd::make_unique
into ndn namespace - add a test suite for
make_unique
Updated by Anonymous about 9 years ago
- Status changed from New to In Progress
- % Done changed from 0 to 20
Updated by Davide Pesavento about 9 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 thestd
namespace, because it will conflict with the ndn-cxx definition, via indirect inclusion ofcommon.hpp
.
Updated by Davide Pesavento about 9 years ago
So I disagree with note 7. We should declare make_unique
in namespace ndn
only.
Updated by Davide Pesavento about 9 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.
Updated by Junxiao Shi about 9 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.
Updated by Junxiao Shi about 9 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:
declare
ndn::make_unique
in util/backports.hppOther backports, such as
ndn::to_string
, can be added to this file in the future.put
#include "util/backports.hpp"
at the end of common.hppUsually 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.test suite should be placed in backports.t.cpp
Updated by Davide Pesavento about 9 years ago
- Assignee changed from Anonymous to Davide Pesavento
Ok, I'm taking over this task if Klaus doesn't mind.
Updated by Davide Pesavento about 9 years ago
- Status changed from In Progress to Code review
- % Done changed from 70 to 100
Updated by Davide Pesavento about 9 years ago
- Status changed from Code review to Closed