stdarray

How should I brace-initialize an std::array of std::pairs?

坚强是说给别人听的谎言 提交于 2019-11-27 08:37:42
std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous` What am I doing wrong? Add another pair of braces. std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } }; std::array<T, N> is an aggregate class containing a member of type T[N] . Usually, you can initialise that the same way you would a plain T[N] array, but when you're dealing with a non-aggregate element type, you may need to be more

Why does std::array not have an constructor that takes a value for the array to be filled with?

泄露秘密 提交于 2019-11-27 07:43:52
Is the absence of std::array<T,size>::array(const T& value); an oversight? It seems mighty useful to me, and dynamic containers (like std::vector ) do have a similar constructor. I am fully aware of std::array<T,size>::fill(const T& value); but that is not a constructor, and the memory will be zeroed out first. What if I want all -1 's like this guy ? std::array is, by design, an aggregate, so has no user-declared constructors. As you say, you could use fill after default constructing. Since it's an aggregate, default construction won't zero the memory, but will leave it uninitialised (if the

Default initialization of std::array?

冷暖自知 提交于 2019-11-27 06:35:14
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 yes. But I would like to be sure of that according to the standard or future standard. By definition,

What is the use of 0-length array (or std::array)?

送分小仙女□ 提交于 2019-11-27 05:06:06
In C++11 it allows you to create a 0 length C array and std:array like this: int arr1[0]; std::array arr2<int,0>; So I'm thinking what is the use of a array that doesn't have a space to store? Secondly what is the zero length array? If it is a pointer, where does it pointing to? Shafik Yaghmour Your first example is not standard C++ but is an extension that both gcc and clang allow, it is version of flexible arrays and this answer to the question: Are flexible array members really necessary? explains the many advantages of this feature. If you compiled using the -pedantic flag you would have

std::array vs array performance

耗尽温柔 提交于 2019-11-27 03:30:22
If I want to build a very simple array like int myArray[3] = {1,2,3}; Should I use std::array instead ? std::array<int, 3> a = {{1, 2, 3}}; What are the advantages of using std::array over usual ones? Is it more performant ? Just easier to handle for copy/access ? What are the advantages of using std::array over usual ones? It has friendly value semantics, so that it can be passed to or returned from functions by value. Its interface makes it more convenient to find the size, and use with STL-style iterator-based algorithms. Is it more performant ? It should be exactly the same. By definition,

std::array with aggregate initialization on g++ generates huge code

霸气de小男生 提交于 2019-11-27 01:48:30
问题 On g++ 4.9.2 and 5.3.1, this code takes several seconds to compile and produces a 52,776 byte executable: #include <array> #include <iostream> int main() { constexpr std::size_t size = 4096; struct S { float f; S() : f(0.0f) {} }; std::array<S, size> a = {}; // <-- note aggregate initialization for (auto& e : a) std::cerr << e.f; return 0; } Increasing size seems to increase compilation time and executable size linearly. I cannot reproduce this behaviour with either clang 3.5 or Visual C++

Passing a std::array of unknown size to a function

时间秒杀一切 提交于 2019-11-26 18:39:56
In C++11, how would I go about writing a function (or method) that takes a std::array of known type but unknown size? // made up example void mulArray(std::array<int, ?>& arr, const int multiplier) { for(auto& e : arr) { e *= multiplier; } } // lets imagine these being full of numbers std::array<int, 17> arr1; std::array<int, 6> arr2; std::array<int, 95> arr3; mulArray(arr1, 3); mulArray(arr2, 5); mulArray(arr3, 2); During my search I only found suggestions to use templates, but those seems messy (method definitions in header) and excessive for what I'm trying to accomplish. Is there a simple

How to create std::array with initialization list without providing size directly [duplicate]

我怕爱的太早我们不能终老 提交于 2019-11-26 18:05:21
问题 This question already has an answer here: How to emulate C array initialization “int arr[] = { e1, e2, e3, … }” behaviour with std::array? 9 answers How can I make a3 compile? int main() { int a1[] = { 1, 2, 3 }; std::array<int, 3> a2 = { 1, 2, 3 }; std::array<int> a3 = { 1, 2, 3 }; } It's very inconvenient, and brittle, to hard-code the size of the array when using an initialization list, especially long ones. Is there any work around? I hope so otherwise I'm disappointed because I hate C

How should I brace-initialize an std::array of std::pairs?

时间秒杀一切 提交于 2019-11-26 17:46:09
问题 std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous` What am I doing wrong? 回答1: Add another pair of braces. std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } }; std::array<T, N> is an aggregate class containing a member of type T[N] . Usually, you can initialise that the same way you would

What is the use of 0-length array (or std::array)?

Deadly 提交于 2019-11-26 11:27:39
问题 In C++11 it allows you to create a 0 length C array and std:array like this: int arr1[0]; std::array arr2<int,0>; So I\'m thinking what is the use of a array that doesn\'t have a space to store? Secondly what is the zero length array? If it is a pointer, where does it pointing to? 回答1: Your first example is not standard C++ but is an extension that both gcc and clang allow, it is version of flexible arrays and this answer to the question: Are flexible array members really necessary? explains