0

So today I was trying to build a length function for an array, and it seems to work. This is my implementation:

//utils.h
template<typename T, unsigned S>
unsigned int length(const T(&arr)[S]) { return S; }

Pretty simple I thought. But when I try to pass an array to a function and then call this length function it gives me the following error:

Patterns.h:5: note mismatched types const T [S] and const byte* {aka const unsigned char*}

or on hover from the intellisense:

no instance of the function template "length" matches the argument list

Implementation of that function:

 //Patterns.h
 #pragma once
 #include "utils.h"
 void displayPattern(const byte pins[], const byte displayData[])
 {
     for (byte i = 0; i < length(displayData); i++)
     {

But the strange thing that I dont understand is why it does work in my other file:

//Project.ino (Yes this is arduino project)
#pragma once
#include "Patterns.h"
const byte leds[] = { 13, 12, 11, 10, 9 };
void setup() {
    for (byte i = 0; i < length(leds); i++) //Does not give a error

Why does it work in the third file, but not in the second file? Does it have to do something with the fact that arrays are passed by reference and not by value?

Any help would be apreciated.

NOTE: byte is the same as unsigned char (in Arduino), it is just used to only represent a number or an 'array' of bits

  • 2
    `displayData` isn't an array, it's a pointer. – Barry Dec 14 '17 at 19:58
  • But why then can I still iterate over pins as if it is an array, displayData is the same kinf of thing, so why wouldn't it work? I am trying to understand. – erik321123 Dec 14 '17 at 20:05
  • It's all in what decays when. `leds` is still an array when you call `length(leds)`, but inside `displayPattern` `const byte displayData[]` has already decayed into a pointer and is no longer an array. You've lost the length and cannot establish `S` for the template specilization. – user4581301 Dec 14 '17 at 20:16
  • C style arrays have some mysterious properties that often confound new users. If you're learning C++, I'd recommend avoiding them and using C++ types, like `std::vector` or `std::array`. – Drew Dormann Dec 14 '17 at 20:19
  • Your function `length` is correct. It's your function `displayPattern` that's incorrect, because there are actually no arrays there. – Drew Dormann Dec 14 '17 at 20:22
  • Is there a way to achieve what I am trying without std::vector (or std::array) I know these are easier to work with, but I am not alowed since this project will be for school in the future. – erik321123 Dec 14 '17 at 20:31
  • Be a sneak and roll your own version of `std::vector`/`std::array`. You will probably learn more from doing that than from what your instructor is trying to teach. – user4581301 Dec 14 '17 at 20:35
  • Problem with that is that I have to use normal arrays in tests on paper as well :/ – erik321123 Dec 14 '17 at 20:51
  • A C array has both **a location and a count**. When it decays to a pointer, you only have **a location**. This is managed in C by passing both the location and count separately. `void displayPattern(const byte pins*, size_t pin_count, const byte displayData*, size_t display_count)` – Drew Dormann Dec 14 '17 at 21:14
  • I got it to work using: `template void displayPattern(const byte (&pins)[U], const byte (&displayData)[S])` Kan someone explain why thís does indeed work? – erik321123 Dec 15 '17 at 18:04

0 Answers0