stdarray

Clang warns me when I aggregate initialize an array while gcc doesn't

主宰稳场 提交于 2019-12-24 03:48:13
问题 When I compile the following piece of code with CLANG: #include <iostream> #include <array> #include <algorithm> #include <functional> int main() { std::array<int, 2> a = {1, 2}; std::array<int, 2> b = {2, 1}; std::array<int, 2> c; std::transform(a.begin(), a.end(), b.begin(), c.begin(), std::multiplies<int>()); for(auto &&i : c) std::cout << i << " "; std::cout << std::endl; } by issuing the command: clang++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp It issues the warning: warning:

Why can't a 2D std::array be initialized with two layers of list-initializers?

蹲街弑〆低调 提交于 2019-12-22 04:06:17
问题 can someone help me understand why my compiler can't/doesn't deduce this? (using g++ 7.3) Does not work: #include <array> std::array<std::array<double,2>,2> f() { return {{0,0},{0,0}}; } Works fine: #include <array> std::array<std::array<double,2>,2> f() { return {std::array<double,2>{0,0},{0,0}}; } Also weirdly this fails too: #include <array> std::array<std::array<double,2>,2> f() { return std::array<std::array<double,2>,2>{{0,0},{0,0}}; } @1201ProgramAlarm pointed out that adding another

Using std::array as Attribute for boost::spirit::x3

江枫思渺然 提交于 2019-12-21 04:40:25
问题 I'm trying to parse a list of numbers into a fixed sized std::array container using boost::spirit's newest release x3 (as included in boost 1.54). Since std::array has the necessary functions, it is detected as an Container, but it is lacking the insert function which makes it incompatible. Here is a short example of what I am trying to accomplish: #include <boost/spirit/home/x3.hpp> #include <array> #include <iostream> #include <string> namespace x3 = boost::spirit::x3; namespace ascii =

Why can't I decrement std::array::end()?

三世轮回 提交于 2019-12-19 05:57:57
问题 I'm creating a convenient display() function template for container types. The output for the last element is different from the rest, thus I check when myIterator != --cont.cend(); . This works for std::vector , but won't work for std::array . Why? Here's a MWE (not my actual code): std::vector<double> vec({1,2}); std::array<double, 2> arr({{1,2}}); auto vecIt = --vec.end(); // OK auto arrIt = --arr.end(); // error: lvalue required as decrement operand 回答1: It depends on how the iterator is

What is the sizeof std::array<char, N>? [duplicate]

橙三吉。 提交于 2019-12-18 07:44:58
问题 This question already has an answer here : Is the size of std::array defined by standard (1 answer) Closed 5 years ago . What does the C++ standard say about what sizeof(std::array<char, N>) should be (for some constant N )? In a comment to a different question, it was mentioned that std::array is not always "stack allocated". The comment was in response to a different comment that speculated that putting a too large of a constant for std::array that is declared as a local variable could

Default initialization of std::array?

做~自己de王妃 提交于 2019-12-17 08:17:15
问题 With C++11 std::array , do I have the guarantee that the syntax std::array<T, N> x; will default-initialize all the elements of the array ? EDIT : if not, is there a syntax that will work on all arrays (including zero-sized arrays) to initialize all elements to their default value? EDIT : on cppreference, the default constructor description says: (constructor) (implicitly declared) (public member function) default-constructs or copy-constructs every element of the array so the answer may be

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

Allocator-aware `std::array`-style container?

别等时光非礼了梦想. 提交于 2019-12-11 07:31:43
问题 I'm writing some code that handles cryptographic secrets, and I've created a custom ZeroedMemory implementation of std::pmr::memory_resource which handles sanitizes memory on deallocation and encapsulates using the magic you have to use to prevent optimizing compilers from eliding away the operation. The idea was to avoid specializing std::array , because the lack of a virtual destructor means that destruction after type erasure would cause memory to be freed without being sanitized.

Access static constexpr std::array without out-of-class definition

懵懂的女人 提交于 2019-12-11 02:38:24
问题 I have a class that defines some arrays. Points.hpp class Points { public: static constexpr std::array< double, 1 > a1 = { { +0.0 } }; static constexpr std::array< double, 2 > a2 = { { -1.0 / std::sqrt( 3.0 ), +1.0 / std::sqrt( 3.0 ) } }; }; My main file then uses these arrays. main.cpp #include "Points.hpp" int main() { // Example on how to access a point. auto point = Points::a2[0]; // Do something with point. } When I compile my code, using C++11 and g++ 4.8.2, I get the following linker

Unneeded conversion happening on std::move

不打扰是莪最后的温柔 提交于 2019-12-10 11:44:00
问题 Why is the compiler thinking I am trying to convert from an object of type Container to an object of type MoveTest in the following code? Why is the braced constructor being forwarded to the move_things variable? Replacing the {} with () of course solves the problem #include <iostream> #include <array> using namespace std; static constexpr int NUMBER{2}; class MoveTest { public: MoveTest(const MoveTest&) { cout << "MoveTest(const MoveTest&)" << endl; } MoveTest(MoveTest&&) { cout << "MoveTest