c++14

Is there a reason for zero sized std::array in C++11?

孤者浪人 提交于 2019-12-12 07:39:03
问题 Consider the following piece of code, which is perfectly acceptable by a C++11 compiler: #include <array> #include <iostream> auto main() -> int { std::array<double, 0> A; for(auto i : A) std::cout << i << std::endl; return 0; } According to the standard § 23.3.2.8 [ Zero sized arrays ]: 1 Array shall provide support for the special case N == 0 . 2 In the case that N == 0 , begin() == end() == unique value. The return value of data() is unspecified. 3 The effect of calling front() or back() for

Why does lambda init-capture not work for unique_ptr?

折月煮酒 提交于 2019-12-12 07:27:34
问题 I'm trying to use the C++14 init-capture feature to move a unique_ptr inside a lambda via capture. For some reason, both gcc and clang refuse to compile my code, insisting that I'm trying to copy a unique_ptr which obviously doesn't work. I thought that avoiding the copy was exactly the point of the init-capture + std::move feature - in fact, passing a unique_ptr seems to be the prime example used by everybody. What am I doing wrong? #include <functional> #include <iostream> #include <memory>

why for-loop isn't a compile time expression and extended constexpr allows for-loop in a constexpr function

杀马特。学长 韩版系。学妹 提交于 2019-12-12 07:18:42
问题 I wrote code like this #include <iostream> using namespace std; constexpr int getsum(int to){ int s = 0; for(int i = 0; i < to; i++){ s += i; } return s; } int main() { constexpr int s = getsum(10); cout << s << endl; return 0; } I understand that it works because of extended constexpr. However in this question why-isnt-a-for-loop-a-compile-time-expression, the author gave his code as follow: #include <iostream> #include <tuple> #include <utility> constexpr auto multiple_return_values() {

What is performance-wise the best way to generate random bools?

青春壹個敷衍的年華 提交于 2019-12-12 07:08:42
问题 I need to generate random Boolean values on a performance-critical path. The code which I wrote for this is std::random_device rd; std::uniform_int_distribution<> randomizer(0, 1); const int val randomizer(std::mt19937(rd())); const bool isDirectionChanged = static_cast<bool>(val); But do not think that this is the best way to do this as I do not like doing static_cast<bool> . On the web I have found a few more solutions 1. std::bernoulli_distribution 2. bool randbool = rand() & 1; Remember

std::function wrapper that avoids dynamic memory allocation

天大地大妈咪最大 提交于 2019-12-12 03:26:17
问题 Consider the following code: #include <functional> #include <cstdio> #include <cstdlib> #include <cstring> #include <time.h> struct Foo { Foo(int x) : x(x) {} void func1() const { printf("Foo::func1 x=%d\n", x); } void func2() const { printf("Foo::func2 x=%d\n", x); } int x; char junk[64]; }; struct Bar { Bar(int x) : x(x) {} void func3() const { printf("Bar::func3 x=%d\n", x); } int x; char junk[64]; }; void loop(std::function<void()>& g) { for (int i=0; i<10; ++i) { switch (rand()%3) { case

Should I move a unique_pointer or should I send a pointer to a unique_pointer?

喜你入骨 提交于 2019-12-11 20:31:46
问题 I am writing a c++ implementation of a BST tree using std::unique_ptr. I am a hobbyist programmer. Initially, I wrote an insert function using a helper function and passing the pointer using move semantics, but that forced me to return unique_ptr. I then considered passing a pointer to the unique_ptr. I even considered using unique_ptr::get() and sending a pointer to the binaryNode, but I have read that this function should only be used when interfacing with legacy functions (Implementation

Invalid operand compiler error involving std::equal_range

為{幸葍}努か 提交于 2019-12-11 17:37:17
问题 For an implementation of an interval map using the following template template<class K, class V> intervalMap{ public: intervalMap(V const&); public: void assign(K const&, K const&, V const&); private: std::map<K,V>m_map; }; And the following code segment inside the member function assign() : template<class K, class V> void intervalMap::assign( K const& keyBegin, K const& keyEnd, V const& val ){ auto begin = m_map.find(keyBegin); auto end = m_map.find(keyEnd); auto p = std::equal_range(begin

Is there / should there be a way to track the validity of a raw pointer taken from a smart pointer for debug/QA purposes?

自作多情 提交于 2019-12-11 17:34:25
问题 As I understand C++ Core Guidelines by Bjarne Stroustrup & Herb Sutter, it is a good idea to use pointers this way: unique_ptr for the good old clear one ownership shared_ptr for truly shared ownership by design weak_ptr when by design the pointer might not be valid anymore raw pointer whenever the pointer is valid by design, and ownership is unchanged Thus theoretically it still may be a common bug that a raw pointer becomes invalid (see R37). I wonder if there is a way for debug/QA purposes

avoid writing the same repetitive type-checking code with std::any

独自空忆成欢 提交于 2019-12-11 15:42:38
问题 I want to use std::any in my program but I find myself writing a lot of conditional statements like this: if (anything.type() == typeid(short)) { auto s = std::any_cast<short>(anything); } else if (anything.type() == typeid(int)) { auto i = std::any_cast<int>(anything); } else if (anything.type() == typeid(long)) { auto l = std::any_cast<long>(anything); } else if (anything.type() == typeid(double)) { auto d = std::any_cast<double>(anything); } else if (anything.type() == typeid(bool)) { auto

Which compiler, if any has a bug in parameter pack expansion?

喜欢而已 提交于 2019-12-11 15:35:32
问题 When experimenting with convenient ways to access tuples as containers, I wrote a test program. on clang (3.9.1, and apple clang) it compiles as expected, producing the expected output: 1.1 foo 2 on gcc (5.4, 6.3), it fails to compile: <source>: In lambda function: <source>:14:61: error: parameter packs not expanded with '...': +[](F& f, Tuple& tuple) { f(std::get<Is>(tuple)); }... ^ <source>:14:61: note: 'Is' <source>: In function 'decltype(auto) notstd::make_callers_impl(std::index_sequence