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