c++17

What do we need std::as_const() for?

吃可爱长大的小学妹 提交于 2020-12-29 08:52:01
问题 C++11 has given us std::add_const; with C++17, we have a new structure - std::as_const(). The former just tacks a const before the type you provide it with. The second one is a proper (template of a) function, not type trait, which seems to do the same - except for when the type is an rvalue-reference, in which case it cannot be used. I don't quite understand the motivation for providing std::as_const() . Why do we need it in addition to std::add_const ? 回答1: "Need" is a strong word... std:

Wrong understanding of 'if constexpr'

不羁的心 提交于 2020-12-26 07:54:19
问题 I have following code static constexpr bool condition = true; int square(int num) { if constexpr (condition) { return num * num; } else { x return num; } } int main() { return square(3); } compiled with -std=gnu++17 My assumption for if constexpr (condition) was that during compilation the part } else { x return num; } get discarded and I get no error about the undefined x Is my understanding wrong that this 'if constexpr' is something like #ifdef CONDITION return num * num; #else x return

Wrong understanding of 'if constexpr'

与世无争的帅哥 提交于 2020-12-26 07:53:48
问题 I have following code static constexpr bool condition = true; int square(int num) { if constexpr (condition) { return num * num; } else { x return num; } } int main() { return square(3); } compiled with -std=gnu++17 My assumption for if constexpr (condition) was that during compilation the part } else { x return num; } get discarded and I get no error about the undefined x Is my understanding wrong that this 'if constexpr' is something like #ifdef CONDITION return num * num; #else x return

Random number from normal distribution in C++

六眼飞鱼酱① 提交于 2020-12-26 07:26:46
问题 As a complete beginner to C++, I would like to generate a random number from a normal distribution. With the following code (derived from this post), I am able to do so: #include <iostream> #include <boost/random.hpp> #include <boost/random/normal_distribution.hpp> using namespace std; int main() { boost::mt19937 rng(std::time(0)+getpid()); boost::normal_distribution<> nd(0.0, 1.0); boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > rnorm(rng, nd); cout<< rnorm(); return

Random number from normal distribution in C++

南楼画角 提交于 2020-12-26 07:26:27
问题 As a complete beginner to C++, I would like to generate a random number from a normal distribution. With the following code (derived from this post), I am able to do so: #include <iostream> #include <boost/random.hpp> #include <boost/random/normal_distribution.hpp> using namespace std; int main() { boost::mt19937 rng(std::time(0)+getpid()); boost::normal_distribution<> nd(0.0, 1.0); boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > rnorm(rng, nd); cout<< rnorm(); return

C++ std::variant vs std::any

*爱你&永不变心* 提交于 2020-12-25 00:19:19
问题 C++17 presents std::variant and std::any, both able to store different type of values under an object. For me, they are somehow similar (are they?). Also std::variant restricts the entry types, beside this one. Why we should prefer std::variant over std::any which is simpler to use? 回答1: The more things you check at compile time the fewer runtime bugs you have. variant guarantees that it contains one of a list of types (plus valueless by exception). It provides a way for you to guarantee that

C++ std::variant vs std::any

点点圈 提交于 2020-12-25 00:18:11
问题 C++17 presents std::variant and std::any, both able to store different type of values under an object. For me, they are somehow similar (are they?). Also std::variant restricts the entry types, beside this one. Why we should prefer std::variant over std::any which is simpler to use? 回答1: The more things you check at compile time the fewer runtime bugs you have. variant guarantees that it contains one of a list of types (plus valueless by exception). It provides a way for you to guarantee that

How to `std::bind()` a standard library algorithm?

好久不见. 提交于 2020-12-24 08:54:01
问题 The short version of my question is this: How can I use something like std::bind() with a standard library algorithm? Since the short version is a bit devoid of details, here is a bit of an explanation: Assume I have the algorithms std::transform() and now I want to implement std::copy() (yes, I realize that there is std::copy() in the standard C++ library). Since I'm hideously lazy, I clearly want to use the existing implementation of std::transform() . I could, of course, do this: struct

How to `std::bind()` a standard library algorithm?

六月ゝ 毕业季﹏ 提交于 2020-12-24 08:53:25
问题 The short version of my question is this: How can I use something like std::bind() with a standard library algorithm? Since the short version is a bit devoid of details, here is a bit of an explanation: Assume I have the algorithms std::transform() and now I want to implement std::copy() (yes, I realize that there is std::copy() in the standard C++ library). Since I'm hideously lazy, I clearly want to use the existing implementation of std::transform() . I could, of course, do this: struct

How to get the size of data stored in “any” in c++17?

你离开我真会死。 提交于 2020-12-15 06:09:27
问题 suppose i have a function like this int writetofile(wstring name, any sdata){ ... return error; } This function have no idea about what data would be stored but would need to know the size of the data stored in sdata . Although it is easy to determine the type of data stored in the sdata but i don't think there is some easy way to know about the size of data in sdata . i have a data structure which has members of type wstring . Now We cant write that data structure directly to the file as it