c++14

Efficiently erase a unique_ptr from an unordered_set

孤人 提交于 2020-04-11 05:47:07
问题 I am storing the ownership of some objects inside an unordered_set , using unique_ptr s. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like this: typedef unique_ptr<MyType> MyPtr; unordered_set<MyPtr> owner; MyPtr p = make_unique<MyType>("foo") MyType *pRaw = p.get(); owner.insert(std::move(p)); // Later ... // I want to do something like this (cannot be written as-is, of course): // owner.erase(pRaw); Is there a way to do this? I can

Efficiently erase a unique_ptr from an unordered_set

时光总嘲笑我的痴心妄想 提交于 2020-04-11 05:44:14
问题 I am storing the ownership of some objects inside an unordered_set , using unique_ptr s. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like this: typedef unique_ptr<MyType> MyPtr; unordered_set<MyPtr> owner; MyPtr p = make_unique<MyType>("foo") MyType *pRaw = p.get(); owner.insert(std::move(p)); // Later ... // I want to do something like this (cannot be written as-is, of course): // owner.erase(pRaw); Is there a way to do this? I can

Can a forwarding reference be aliased with an alias template?

◇◆丶佛笑我妖孽 提交于 2020-03-21 20:29:39
问题 This is a continuation of my previous question: Can an identity alias template be a forwarding reference? It seems that the following code works in both Clang 3.7.0 (demo) and GCC 6.0.0 (demo): template <class T> using forwarding_reference = T&&; template <class T> void foo(forwarding_reference<T>) {} int main() { int i{}; foo(i); foo(1); } Are the compilers right to substitute the alias template for a forwarding reference and this could be a fancy way of writing one? 回答1: This is indeed

Partial template specialization with mismatching `int` and `size_t` not compiling

為{幸葍}努か 提交于 2020-03-21 11:33:57
问题 With reference to the following code #include <utility> #include <cassert> template <typename T> struct Wot; template <int... ints> struct Wot<std::index_sequence<ints...>> {}; int main() { assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1); } This works on clang but does not work on gcc, when I change the type of the partial specialization to accept std::size_t in the index sequence however it works. Who is right? Clang or gcc? See this in action here https://wandbox.org/permlink

Partial template specialization with mismatching `int` and `size_t` not compiling

百般思念 提交于 2020-03-21 11:33:06
问题 With reference to the following code #include <utility> #include <cassert> template <typename T> struct Wot; template <int... ints> struct Wot<std::index_sequence<ints...>> {}; int main() { assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1); } This works on clang but does not work on gcc, when I change the type of the partial specialization to accept std::size_t in the index sequence however it works. Who is right? Clang or gcc? See this in action here https://wandbox.org/permlink

C++ constexpr : Compute a std array at compile time

对着背影说爱祢 提交于 2020-03-17 08:54:01
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

C++ constexpr : Compute a std array at compile time

半城伤御伤魂 提交于 2020-03-17 08:52:04
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

Passing different lambdas to function template in c++

泪湿孤枕 提交于 2020-03-01 14:49:47
问题 I have a class Foo that accepts different predicate variants through its constructor. template<typename T> struct Value { T value; }; class Foo { public: template<typename T> Foo(Value<T> &value, function<bool()> predicate) { } template<typename T> Foo(Value<T> &value, function<bool(const Value<T> &)> predicate) : Foo(value, function<bool()>([&value, predicate](){ return predicate(value); })) { } }; This allows me to construct the class with explicit function object: Value<int> i; Foo foo0(i,

c++11 constructor with variadic universal references and copy constructor

时光怂恿深爱的人放手 提交于 2020-03-01 02:33:07
问题 How to declare copy constructor, if we have constructor with universal reference arguments, also? http://coliru.stacked-crooked.com/a/4e0355d60297db57 struct Record{ template<class ...Refs> explicit Record(Refs&&... refs){ cout << "param ctr" << endl; } Record(const Record& other){ // never called cout << "copy ctr" << endl; } Record(Record&& other){ // never called cout << "move ctr" << endl; } }; int main() { Record rec("Hello"); Record rec2(rec); // do "param ctr" return 0; } According to

Member initializer list: initialize two members from a function returning a tuple

本小妞迷上赌 提交于 2020-02-29 18:12:09
问题 Can multiple members be initialized in the member initializer list from a tuple obtained by a function? With returning multiple values via tuples becoming more popular I hope there is a solution for this. I see no reason other than a language limitation why this would not be possible. This is a mcve for what I have: auto new_foo(std::size_t size) -> std::tuple<std::unique_ptr<char[]>, int*> { auto buffer = std::make_unique<char[]>(size * sizeof(int) + 8); auto begin = static_cast<int*>(static