c++11

Explicitly use defaults for some parameters in class template instantiation

半世苍凉 提交于 2021-02-18 08:56:30
问题 A class template can have multiple parameters that all have defaults. template<typename UnderlyingT0 = int, typename UnderlyingtT1 = long, typename StringT = std::string> struct options; Instatiating the template with just default parameters is easy: options<> my_default_options; But what if I want to change a subset of parameters? options<int, int, std::wstring> wstring_options; It is not obvious that int is a default for the first parameter while for the second it isn't. Is there something

Efficient accumulate

故事扮演 提交于 2021-02-18 06:18:11
问题 Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector<std::string> foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){ return rs+arg; }); I can be pretty sure there will be temporary object construction. In this answer they say that the effect of std::accumulate is specified this way: Computes its result by initializing the accumulator acc with the initial value init and

Efficient accumulate

我的梦境 提交于 2021-02-18 06:18:07
问题 Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector<std::string> foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){ return rs+arg; }); I can be pretty sure there will be temporary object construction. In this answer they say that the effect of std::accumulate is specified this way: Computes its result by initializing the accumulator acc with the initial value init and

Forbid integer conversion with precision loss

荒凉一梦 提交于 2021-02-18 06:13:21
问题 How to prevent such code from compiling? #include <vector> #include <limits> #include <iostream> #include <cstdint> int main() { std::vector<int16_t> v; v.emplace_back(std::numeric_limits<uint64_t>::max()); std::cout << v.back() << std::endl; return 0; } g++ and clang with -std=c++14 -Wall -Wextra -Werror -pedantic -Wold-style-cast -Wconversion -Wsign-conversion don't even warn about it. The example also compiles without warnings with std::vector<uint16_t> 回答1: Add -Wsystem-headers to the

Using an atomic read-modify-write operation in a release sequence

☆樱花仙子☆ 提交于 2021-02-17 21:49:06
问题 Say, I create an object of type Foo in thread #1 and want to be able to access it in thread #3. I can try something like: std::atomic<int> sync{10}; Foo *fp; // thread 1: modifies sync: 10 -> 11 fp = new Foo; sync.store(11, std::memory_order_release); // thread 2a: modifies sync: 11 -> 12 while (sync.load(std::memory_order_relaxed) != 11); sync.store(12, std::memory_order_relaxed); // thread 3 while (sync.load(std::memory_order_acquire) != 12); fp->do_something(); The store/release in thread

What's the proper way to associate a mutex with its data?

杀马特。学长 韩版系。学妹 提交于 2021-02-17 19:41:31
问题 In the classic problem of transferring money from one bank account to another, the accepted solution (I believe) is to associate a mutex with each bank account, then lock both before withdrawing the money from one account and depositing it into the other. At first blush, I'd do it like this: class Account { public: void deposit(const Money& amount); void withdraw(const Money& amount); void lock() { m.lock(); } void unlock() { m.unlock(); } private: std::mutex m; }; void transfer(Account& src,

What's the proper way to associate a mutex with its data?

空扰寡人 提交于 2021-02-17 19:40:06
问题 In the classic problem of transferring money from one bank account to another, the accepted solution (I believe) is to associate a mutex with each bank account, then lock both before withdrawing the money from one account and depositing it into the other. At first blush, I'd do it like this: class Account { public: void deposit(const Money& amount); void withdraw(const Money& amount); void lock() { m.lock(); } void unlock() { m.unlock(); } private: std::mutex m; }; void transfer(Account& src,

Partial template function specialization with enable_if: make default implementation

こ雲淡風輕ζ 提交于 2021-02-17 18:54:10
问题 Using C++11's enable_if I want to define several specialized implementations for a function (based on the type of the parameter, say) as well as a default implementation. What is the correct way to define it? The following example does not work as intended since the "generic" implementation is called, whatever the type T . #include <iostream> template<typename T, typename Enable = void> void dummy(T t) { std::cout << "Generic: " << t << std::endl; } template<typename T, typename std::enable

What is the type of a pointer to a 2D array?

谁都会走 提交于 2021-02-17 15:15:33
问题 I know that the following is not correct: int arr[2][3] = {}; //some array initialization here int** ptr; ptr = arr; But I am quite surprised that the following lines actually work int arr[2][3] = {}; //some array initialization here auto ptr = arr; int another_arr[2][3] = {}; //some array initialization here ptr = another_arr; Can anyone possibly explain what is the type assigned to ptr in the second block of code, and what happened underneath? 回答1: Well, arrays decay to pointers when used

What is the type of a pointer to a 2D array?

社会主义新天地 提交于 2021-02-17 15:14:55
问题 I know that the following is not correct: int arr[2][3] = {}; //some array initialization here int** ptr; ptr = arr; But I am quite surprised that the following lines actually work int arr[2][3] = {}; //some array initialization here auto ptr = arr; int another_arr[2][3] = {}; //some array initialization here ptr = another_arr; Can anyone possibly explain what is the type assigned to ptr in the second block of code, and what happened underneath? 回答1: Well, arrays decay to pointers when used