Initialize array of arrays

Hey,

I don't know how to copy values from an array into an array of arrays, what am i doing wrong? I have an array of bools, now i want to create 8 of them
This code doesn't work: (the atomics are used for having multiple instances)
The error: Overload resolution selected deleted operator '='



1
2
3
4
5
6
7
 std::atomic<int> g_activeTracksXMLArray[8][16];  
std::atomic<bool> g_activeTracksXML[16] {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  for (int i = 0; i < 8; i++) {
 
    std::copy(std::begin(g_activeTracksXML),
                std::end(g_activeTracksXML),
           std::begin(g_activeTracksXMLArray[i]));
Last edited on
This code compiles OK with VS2022 as C++23. What compiler are you using?

1
2
3
4
5
6
7
8
9
10
11
#include <algorithm>
#include <atomic>

int main() {
	std::atomic<int> g_activeTracksXMLArray[8][16];
	std::atomic<bool> g_activeTracksXML[16] { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };

	for (int i = 0; i < 8; i++) {
		std::copy(std::begin(g_activeTracksXML), std::end(g_activeTracksXML), std::begin(g_activeTracksXMLArray[i]));
	}
}

Indeed it works sorry, I have a bunch of them, this actually fails:
1
2
3
4
5
6
7
8
std::atomic<int> g_channelNumbersXML[16] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
std::atomic<int> g_channelNumbersXMLArray[8][16];

for (int i = 0; i < 8; i++) {
 std::copy(std::begin(g_channelNumbersXML),
                std::end(g_channelNumbersXML),
                std::begin(g_channelNumbersXMLArray[i]));
}
Last edited on
OK. Both variables in an = operation cannot be std::atomic i.e LHS and RHS as it is not possible to read and write atomically in one instruction. See:
https://stackoverflow.com/questions/60901196/c-atomic-variables-cannot-be-assigned-to-each-other-why-is-it-possible-to

std::copy uses = operation so can't be done with both sides being atomic int.

ok tx!! Do I need to loop over them to copy them? They both need to be atomic actually
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <atomic>

template < typename T, typename U, std::size_t N >
void copy( std::atomic<T>(&srce)[N], std::atomic<U>(&dest)[N] ) {

   for( std::size_t i = 0 ; i < N ; ++i ) dest[i] = T(srce[i]) ;
}

int main()
{
    std::atomic<int> g_channelNumbersXML[16] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    std::atomic<int> g_channelNumbersXMLArray[8][16];
    std::atomic<long> g_channelNumbersXMLArray_2[8][16];

    for (int i = 0; i < 8; i++) copy( g_channelNumbersXML, g_channelNumbersXMLArray[i] ) ;
    for (int i = 0; i < 8; i++) copy( g_channelNumbersXML, g_channelNumbersXMLArray_2[i] ) ;
}

As g_channelNumbersXML is only read from, why does it need to be atomic and why not constexpr?
It's not only read from, during the runtime the values van change.
@jlBorges, why is the long in there? Is it for demo or do I need it?
It's just for test/demo showing copying from an int array to a long array using the same templated copy() function as for int to int.

The 'magic' is the cast T(srce[i]) on L6.
Last edited on
Pure genious! Thank you everyone!
Note that the original code could be re-written using std::transform instead of std::copy as:

1
2
3
4
5
6
7
8
9
int main() {
	std::atomic<int> g_channelNumbersXML[16] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
	std::atomic<int> g_channelNumbersXMLArray[8][16];

	for (auto& d : g_channelNumbersXMLArray)
		std::transform(std::begin(g_channelNumbersXML),
			std::end(g_channelNumbersXML),
			std::begin(d), [](const auto& i) {return static_cast<int>(i); });
}


and the copy() function could also use std::transform

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <atomic>
#include <algorithm>

template < typename T, typename U, std::size_t N >
void copy(const std::atomic<T>(&srce)[N], std::atomic<U>(&dest)[N]) {
	std::transform(std::begin(srce), std::end(srce), std::begin(dest), [](const auto& i) {return static_cast<T>(i); });
}

int main() {
	std::atomic<int> g_channelNumbersXML[16] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
	std::atomic<int> g_channelNumbersXMLArray[8][16];

	for (auto& d : g_channelNumbersXMLArray)
		copy(g_channelNumbersXML, d);
}

Last edited on
Out of curiosity, what is the difference between copy, fill and transform? :)
Out of curiosity, what is the difference between copy, fill and transform? :)

Take a look at this video, which summarizes all the algorithms as of C++17:
https://www.youtube.com/watch?v=2olsGf6JIkU
At least seek out some resource that will explain what's available. For example you could browse the standard draft:
https://eel.is/c++draft/#algorithms
Last edited on
fill assigns a fixed value to each element in a range.
copy copies elements from one range to another.
transform is similar to copy but instead of copy each value as-is it passes each value to a function and assigns the retuned value.

Note that all of these use assignment (=) to assign new values. The size of the ranges does not (and cannot) change.
Last edited on
These are part of Algorithms. See:
https://en.cppreference.com/w/cpp/algorithm
Tx again!
Topic archived. No new replies allowed.