Does the Photon support C++11 things like valarray?

I’m trying to build a scrolling line graph. I figured arrays are the best way to do that. I need to shift the entire contents of the array over to the left one for the graph to scroll. Google says that “valarray” is the best way to do this. As soon as I include valarray in my project, I get a whole bunch of the weirdest compiler errors I’ve ever seen. Referencing directories I didn’t even know existed.

I’m not sure how to copy compiler errors, so I guess I’ll just have to link an image:

To be clear, even though those /usr/local are linux directories, I’m on Windows. As you can see it’s also throwing up errors in other included libraries, errors that I didn’t have before. All because I added the line #include “valarray” into my code. Now I know my understanding of C++ is the programming equivalent of a dog trained how to pilot a space shuttle - I don’t really know what I’m doing, I’m just good at figuring things out by context - but is this even possible on the Photon since it is C++11? Am I doing something wrong, or missing another include perhaps?

Hmm… valarray works for me. I think you need to show the code you’re using.

#include "valarray"

 std::valarray<int> myValArray = {1,2,3,4,5};  

void setup() {
  Serial.begin(9600);
  delay(3000);

  myValArray = myValArray.cshift(2); // 3 4 5 1 2 - rotated left
  for (int i=0; i<5; i++) {
	  Serial.println(myValArray[i]);
  }
}

I’ve never used this object before, so I don’t know if it’s the best way to accomplish your goal, but it does work on a Photon.

1 Like

Crap. You’re right, I just tried a new window with just that code, and it compiled fine. Well it’s way too much code to post here with many libraries and even more subclassed libraries. This is just the first time I’ve ever encountered a situation where just including a standard C++ library can somehow break everything. I haven’t even tried to use any of the valarray functions in my code yet. I just take my code that currently just subscribes to my other Photon and draws a bunch of stuff on an LCD, that works perfectly fine on its own, and as soon as I include valarray, I get all these really weird compiler errors. I can’t ask you to pour through all this code trying to figure out what went wrong, there’s just too much, and I can’t understand the compiler errors enough to know where to begin to look myself. Maybe one of my libraries is declaring a function name identical to one existing in valarray? I think I’m going to have to find a different way to do this.

You might just want to use a circular buffer, which is really just a normal array where you keep track of the starting and ending positions in the array. So instead of reading out the values from 0 to 10 say, you start reading them at 1, go to 10, then around to 0. This is much more memory efficient, since you’re not creating new arrays, you’re only changing the way you think of where the start and end of your array is. I’m not sure this is what you want, since I don’t know exactly what you mean be a scrolling line graph.

I think that swap (and previously min and max) are/were implemented as macros in Arduino-land, so if you run across this again, I would try #undef swap before the include statement for whatever C++ library you are trying to use.

2 Likes

Hey that did the trick, thanks bko!

1 Like