c++11

std::atomic_is_lock_free(shared_ptr<T>*) didn't compile

╄→гoц情女王★ 提交于 2021-02-08 14:11:13
问题 My simple code like this: #include <iostream> #include <atomic> #include <memory> int main(void) { std::shared_ptr<int> p = std::make_shared<int>(5); std::cout << std::boolalpha << std::atomic_is_lock_free(&p) << std::endl; return 0; } But compile error: a.cpp: In function 'int main()': a.cpp:7:60: error: no matching function for call to 'atomic_is_lock_free(std::shared_ptr*)' std::cout << std::boolalpha << std::atomic_is_lock_free(&p) << std::endl; ^ a.cpp:7:60: note: candidates are: In file

std::atomic_is_lock_free(shared_ptr<T>*) didn't compile

狂风中的少年 提交于 2021-02-08 14:11:07
问题 My simple code like this: #include <iostream> #include <atomic> #include <memory> int main(void) { std::shared_ptr<int> p = std::make_shared<int>(5); std::cout << std::boolalpha << std::atomic_is_lock_free(&p) << std::endl; return 0; } But compile error: a.cpp: In function 'int main()': a.cpp:7:60: error: no matching function for call to 'atomic_is_lock_free(std::shared_ptr*)' std::cout << std::boolalpha << std::atomic_is_lock_free(&p) << std::endl; ^ a.cpp:7:60: note: candidates are: In file

std::atomic_is_lock_free(shared_ptr<T>*) didn't compile

爱⌒轻易说出口 提交于 2021-02-08 14:09:35
问题 My simple code like this: #include <iostream> #include <atomic> #include <memory> int main(void) { std::shared_ptr<int> p = std::make_shared<int>(5); std::cout << std::boolalpha << std::atomic_is_lock_free(&p) << std::endl; return 0; } But compile error: a.cpp: In function 'int main()': a.cpp:7:60: error: no matching function for call to 'atomic_is_lock_free(std::shared_ptr*)' std::cout << std::boolalpha << std::atomic_is_lock_free(&p) << std::endl; ^ a.cpp:7:60: note: candidates are: In file

Mixing pass-by-reference and pass-by-value to variadic template function valid?

故事扮演 提交于 2021-02-08 14:05:46
问题 I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } Is it valid using this function to mix pass-by-value and pass-by-reference? For example allocating a class with a constructor with some by-value and some by-reference. It compiles, but I'm not sure if it has any nasty side-effects or not. 回答1: What

Is the complexity of unordered_set::find predictable?

爱⌒轻易说出口 提交于 2021-02-08 13:54:58
问题 While looking for a container suitable for an application I'm building, I ran across documentation for unordered_set . Given that my application typically requires only insert and find functions, this class seems rather attractive. I'm slightly put off, however, by the fact that find is O(1) amortized, but O(n) worst case - I would be using the function frequently, and it could make or break my application. What causes the spike in complexity? Is the likelihood of running into an O(n) search

When should I declare a move constructor without noexcept?

拟墨画扇 提交于 2021-02-08 13:37:11
问题 The standard doesn't enforce noexcept on move constructors. In what circumstances is it acceptable/neccesary for a move constructor to throw? 回答1: When you really have no choice. Most of the time your move constructor should be noexcept . And they are by default. See this: http://www.codingstandard.com/rule/12-5-4-declare-noexcept-the-move-constructor-and-move-assignment-operator/ It is especially important to use noexcept for types that are intended to be used with the standard library

why memory_order_relaxed performance is the same as memory_order_seq_cst

混江龙づ霸主 提交于 2021-02-08 13:23:30
问题 I've created a simple test to check how std::memory_order_relaxed is faster than std::memory_order_seq_cst value for atomic<int> increment. However the performance was the same for both cases. My compiler: gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) Build arguments: g++ -m64 -O3 main.cpp -std=c++17 -lpthread CPU: Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz, 4 core, 2 thread per core Test code: #include <vector> #include <iostream> #include <thread> #include <atomic> #include <chrono>

std::hash value on char* value and not on memory address?

筅森魡賤 提交于 2021-02-08 12:37:14
问题 As stated in this link: There is no specialization for C strings. std::hash produces a hash of the value of the pointer (the memory address), it does not examine the contents of any character array. Which means that with the same char* value, different hashcodes could be produced. For example, having this code: //MOK and MOV are template arguments void emit(MOK key, MOV value) { auto h = hash<MOK>()(key); cout<<"key="<<key<<" h="<<h<<endl; ... This is the output produced by calling 4 times

Can you capture arrays in a lambda?

随声附和 提交于 2021-02-08 12:19:35
问题 I'm in a situation in a multithreaded environment where I have a thread that receives data from a socket, and I want to send that data into a messaging queue. For instance, something like this: char buf[N]; size_t len = ::recv(buf, ...); queue.send([buf,len] { //stuff }); But that won't work since buf could go out of scope, or get overwritten by the next ::recv() . Now I COULD copy it into a string / std::vector /whatever and pass THAT thing by value: char buf[N]; size_t len = ::recv(buf, ...

Is 'typedef' automatically inherited in C++ class?

别来无恙 提交于 2021-02-08 12:19:26
问题 I used to believe that 'typedef' is not automatically inherited. But the code snap below suggests something different. #include <iostream> #include <type_traits> struct A { typedef int X; }; struct A_ { typedef char X; }; struct B : A {}; struct B_ : A, A_ {}; template< typename ... Ts > using void_t = void; template< typename T, typename = void > struct has_typedef_X : std::false_type {}; template< typename T > struct has_typedef_X< T, void_t<typename T::X> > : std::true_type {}; int main()