c++14

make_unique with brace initialization

不羁的心 提交于 2020-06-13 19:08:57
问题 https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } This does not work for plain structs with no constructors. Those can be brace-initialized but don't have a non-default constructor. Example: #include <memory> struct point { int x, z; }; int main() { std::make_unique<point>(1, 2)

make_unique with brace initialization

半世苍凉 提交于 2020-06-13 19:07:33
问题 https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } This does not work for plain structs with no constructors. Those can be brace-initialized but don't have a non-default constructor. Example: #include <memory> struct point { int x, z; }; int main() { std::make_unique<point>(1, 2)

Why move return an rvalue reference parameter need to wrap it with std::move()?

安稳与你 提交于 2020-06-12 05:27:25
问题 I was reading Effective Modern C++ Item 25, on page 172, it has an example to demonstrate that, if you want to move return an rvalue reference parameter, you need to wrap it with std::move(param). As parameter by itself is always an lvalue, if no std::move(), it will be copy returned. I don't understand. If std::move(param) merely cast the parameter it takes in into an rvalue reference, then what's the difference when param is already an rvalue reference? Like in the code below: #include

Recursive computation using variable templates - gcc vs clang

非 Y 不嫁゛ 提交于 2020-06-11 16:53:09
问题 Consider the following example: #include <cstdio> template <int N> int fib = fib<N - 1> + fib<N - 2>; template <> int fib<2> = 1; template <> int fib<1> = 1; int main() { std::printf("%d %d %d", fib<4>, fib<5>, fib<6>); } GCC 7.x, 8.x, 9.x, and 10.x all print out the expected result of 3 5 8 . Clang 5.x, 6.x, 7.x, 8.x, 9.x, and 10.x all print out 1 3 4 as a result. live example on godbolt.org Clang's behavior is surprising. Is there any subtle interaction between variable template

How to get all occurences by following algorithms because they shows only the first one?

我们两清 提交于 2020-06-01 05:36:05
问题 I have a problem with the following algorithms. They show to me only the first occurrence but, I want to show all of them. What should we change in order to get all occurrences? And can we use a Plain-Table on Sunday Algorithms in case of Unordered Map? Thank You :) int BruteForce::search(string haystack, string needle) { if (haystack.length() < needle.length()) { return -1; } for (int i = 0; i <= haystack.length() - needle.length(); i++) { int j = 0; while((j < needle.length()) && (haystack

Handing over locked std::unique_lock to new threads

谁都会走 提交于 2020-05-27 07:26:10
问题 Consider the following example where I create a std::mutex , lock it and then hand the lock over to another thread : #include <future> #include <mutex> int main() { // Create and lock a mutex std::mutex mutex; std::unique_lock<decltype(mutex)> lock(mutex); // Hand off the lock to another thread auto promise = std::async(std::launch::async, [lock{ std::move(lock) }]() mutable { // Unlock the mutex lock.unlock(); }); promise.get(); return 0; } The example seems to run fine with gcc 6.3 but

Handing over locked std::unique_lock to new threads

丶灬走出姿态 提交于 2020-05-27 07:24:40
问题 Consider the following example where I create a std::mutex , lock it and then hand the lock over to another thread : #include <future> #include <mutex> int main() { // Create and lock a mutex std::mutex mutex; std::unique_lock<decltype(mutex)> lock(mutex); // Hand off the lock to another thread auto promise = std::async(std::launch::async, [lock{ std::move(lock) }]() mutable { // Unlock the mutex lock.unlock(); }); promise.get(); return 0; } The example seems to run fine with gcc 6.3 but

Can a variable template be passed as a template template argument?

☆樱花仙子☆ 提交于 2020-05-23 04:20:42
问题 The following nonsensical example does not compile, but is there some other way to pass a variable template as a template template argument? template<typename T> constexpr auto zero = T{0}; template<typename T, template<typename> auto VariableTemplate> constexpr auto add_one() { return VariableTemplate<T> + T{1}; } int main() { return add_one<int, zero>(); } Try on Compiler Explorer 回答1: Short answer: No. Long answer: Yes you can using some indirection through a class template: template

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

血红的双手。 提交于 2020-05-16 08:04:33
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

倖福魔咒の 提交于 2020-05-16 08:04:21
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =