c++14

Cast raw pointer of array to unique_ptr

走远了吗. 提交于 2019-12-10 11:26:43
问题 I am working against a blackbox framework (cdg), which fills an array of uint32_t with values. The call looks like that: std::size_t dataCount = 100; uint32_t* data = new uint32_t[dataCount]; cdg.generate(data); Unfortunately, the framework doesn't use templates, so I have to pass in a uint32_t* . To get rid of the raw pointer I want to "wrap" it into a std::unique_ptr<uint32_t> . Thus it is an array I think I have to use a std::unique_ptr<uint32_t[]> . Is there a way to convert the raw

How to instantiate a member function pointer?

心已入冬 提交于 2019-12-10 11:03:53
问题 Lets say I have the follow code template<class MemberFunc> class Foo { MyClass object_; void call() { auto ptr = MemberFunc{}; (object_.*ptr)(); } }; int main() { Foo<decltype(&MyClass::doThings)> foo; foo.call(); } This code does crash for me because ptr is 0. Why does the member function constructor returns 0? My workaround is the following but it involves code duplication. Is there no other way to just construct/instantiate the member function from the type? C++14 welcome. template<class

Are these vector definitions “constant initialization”?

风格不统一 提交于 2019-12-10 10:56:22
问题 This question is about the code (at namespace scope): std::vector<int> v1; std::vector<int> v2(4); In section 3.6.2 of C++14 (N4140) there is defined a term Constant initialization : Constant initialization is performed: [omitted - about reference initialization] if an object with static or thread storage duration is initialized by a constructor call, and if the initialization full-expression is a constant initializer for the object; if an object with static or thread storage duration is not

Does the compiler generate a different type for each lambda?

醉酒当歌 提交于 2019-12-10 10:16:22
问题 Disclaimer: Do not use the code in this question. It invokes undefined behaviour. The core statement of the question, whether the compiler generates a new type for each lambda, and the corresponding answer remain valid. To take get a function pointer to a lambda with a capture I came up with the following trick: auto f = [&a] (double x) { return a*x; }; static auto proxy = f; double(*ptr)(double) = [] (double x) { return proxy(x); }; // do something with ptr I assign the lambda with a capture

Dependant non-type template parameter and variadic template

此生再无相见时 提交于 2019-12-10 10:15:18
问题 I am trying to extend the possibilities offered by std::integer_sequence with a new class named integer_range (which obiously creates a sequence of integers between two bounds). My implementation was based of my answer to this question that I tried to adapt to std::integer_sequence : namespace details { template<typename Int, Int C, Int P, Int... N> struct increasing_integer_range: increasing_integer_range<Int, C-1, P+1, N..., P> {}; template<typename Int, Int C, Int P, Int... N> struct

pragma STDC FENV_ACCESS ON is not supported

て烟熏妆下的殇ゞ 提交于 2019-12-10 09:24:32
问题 I tried to slightly modify the example from the article: #include <iostream> #include <cfenv> #pragma STDC FENV_ACCESS ON int main() { std::feclearexcept(FE_ALL_EXCEPT); //int r = std::feraiseexcept(FE_UNDERFLOW | FE_DIVBYZERO); double x = 1.0; double y = 0.0; double result{}; asm volatile ("fldl %1\n" "fdivl %2\n" : "=%t"(result) : "m"(x), "m"(y) : "memory"); std::cout << result << std::endl; int e = std::fetestexcept(FE_ALL_EXCEPT); if (e & FE_DIVBYZERO) { std::cout << "division by zero\n";

constexpr reference to non-const object

你。 提交于 2019-12-10 09:24:16
问题 Is it permitted to declare a non-const reference as constexpr ? Example code: int x = 1; constexpr int& r = x; This is accepted by gcc and clang (I tried several current and past versions of both, back to C++11, and all accepted it). However I think it should not be accepted because C++14 [dcl.constexpr/9] says: if a constexpr specifier is used in a reference declaration, every full- expression that appears in its initializer shall be a constant expression and x is not a constant expression.

Lambda expressions as class template parameters in C++14

ε祈祈猫儿з 提交于 2019-12-10 04:32:21
问题 The question Lambda expressions as class template parameters asks about the possibility of using lambda expressions as class template parameters. The answer to the question was no. However, it was about C++11. Has the situation changed in the new standard, C++14? 回答1: No the situation in C++14 has not changed at all and in fact the language in section 5.1.2 Lambda expressions paragraph 2 has been tightened from: A lambda-expression shall not appear in an unevaluated operand (Clause 5). to: [.

Nested aggregate initialization of std::array [duplicate]

跟風遠走 提交于 2019-12-10 03:49:14
问题 This question already has an answer here : When can outer braces be omitted in an initializer list? (1 answer) Closed 2 years ago . I wonder, why declaration of std_arr in the following code generates an error, while c_arr compiles well: struct S { int a, b; }; S c_arr[] = {{1, 2}, {3, 4}}; // OK std::array<S, 2> std_arr = {{1, 2}, {3, 4}}; // Error: too many initializers Both std::array and S are aggregates. From aggregate initialization on cppreference.com: If the initializer clause is a

Why does std::cbegin return the same type as std::begin

…衆ロ難τιáo~ 提交于 2019-12-10 03:39:33
问题 cppreference shows this signature for std::cbegin : template< class C > constexpr auto cbegin( const C& c ) -> decltype(std::begin(c)); Shouldn't it return something like C::const_iterator instead? 回答1: c is a const reference, so std::begin(c) it will return whatever the const overload of C::begin() returns. For standard library types, this is a const_iterator . For an array type, it is a pointer to const . Note that this relies on other, non-standard library user defined C , being