std

Proper C++ way of implementing a lock_guard for custom library

一曲冷凌霜 提交于 2020-08-26 10:44:05
问题 O wise interwebs We have an impasse between two colleagues that we could use your help resolving in the proper C++ way. Basically we have a set of utility classes, two of which are a Mutex and SpinLock class which both have the following abridged interface: class Mutex { public: Mutex(); ~Mutex(); void Lock(); void Unlock(); // ... }; Obviously this is similar to, but differently-cased than the BasicLockable concept used by std::lock_guard, so we want something similar (assume that the Mutex

typedef function is not a type name?

无人久伴 提交于 2020-08-26 08:59:25
问题 This is my code inside myCode.h : #include <set> using namespace std; bool MyObjectComp(const MyObject& lhs, const MyObject& rhs) { return lhs.mTick < rhs.mTick; } typedef std::multiset<MyObject, MyObjectComp> MyObjectMultiSet; but it says that function MyObjectComp is not a type name. Where should I place it? 回答1: The template parameter for std::multiset expects a type, MyObjectComp is not a type but is instead a function name. You can either use decltype to get its type like typedef std:

Why does my srand(time(NULL)) function generate the same number every time in c? [duplicate]

旧城冷巷雨未停 提交于 2020-08-19 17:46:44
问题 This question already has answers here : Random numbers in C (10 answers) How can I generate different random numbers for each player? (3 answers) Closed 2 years ago . So I was creating a program that would call a function and return 0 or 1 (0 meaning tails and 1 meaning heads) and then use that to print the outcome of 100 flips. It seemed simple enough thinking I could use srand(time(NULL)) to seed rand() with constantly varying seeds. Here was my first crack. #include <stdio.h> #include

Why does my srand(time(NULL)) function generate the same number every time in c? [duplicate]

南笙酒味 提交于 2020-08-19 17:46:42
问题 This question already has answers here : Random numbers in C (10 answers) How can I generate different random numbers for each player? (3 answers) Closed 2 years ago . So I was creating a program that would call a function and return 0 or 1 (0 meaning tails and 1 meaning heads) and then use that to print the outcome of 100 flips. It seemed simple enough thinking I could use srand(time(NULL)) to seed rand() with constantly varying seeds. Here was my first crack. #include <stdio.h> #include

Vector from long hex value

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-10 18:13:03
问题 In C++ I can initialize a vector using std::vector<uint8_t> data = {0x01, 0x02, 0x03}; For convenience (I have python byte strings that naturally output in a dump of hex), I would like to initialize for a non-delimited hex value of the form: std::vector<uint8_t> data = 0x229597354972973aabbe7; Is there a variant of this that is valid c++? 回答1: Combining comments from Evg, JHbonarius and 1201ProgramAlarm: The answer is that there is no direct way to group but a long hex value into a vector,

Deprecated std::is_literal_type in C++17

旧城冷巷雨未停 提交于 2020-08-01 09:29:12
问题 According to cppreference, the trait std::is_literal_type is deprecated in C++17. The question is why and what is the preferred replacement for the future to check whether a type is a literal type. 回答1: As stated in P0174: The is_literal type trait offers negligible value to generic code, as what is really needed is the ability to know that a specific construction would produce constant initialization. The core term of a literal type having at least one constexpr constructor is too weak to be

Deprecated std::is_literal_type in C++17

南笙酒味 提交于 2020-08-01 09:25:01
问题 According to cppreference, the trait std::is_literal_type is deprecated in C++17. The question is why and what is the preferred replacement for the future to check whether a type is a literal type. 回答1: As stated in P0174: The is_literal type trait offers negligible value to generic code, as what is really needed is the ability to know that a specific construction would produce constant initialization. The core term of a literal type having at least one constexpr constructor is too weak to be

std::unordered_map initialization

拈花ヽ惹草 提交于 2020-06-24 22:10:23
问题 When I access an element in std::unordered_map using operator [] for the first time, it is automatically created. What (if any) are guarantees about its initialization? (It is guaranteed to be value initialized, or only to be constructed)? Example: std::unordered_map<void *, size_t> size; char *test = new char[10]; size[test] += 10; Is size[test] guaranteed to be 10 at the end of this sequence? 回答1: Is size[test] guaranteed to be 10 at the end of this sequence? Yes. In the last line of your