I'm working with a custom C++ library, CPSTL, for Arduino, which includes a cpstd::vector
class that is designed to work with cpstd::initializer_list
, cpstd::initializer_list
is supposed to mimic std::initializer_list
.
However, I'm having trouble initializing a cpstd::vector
using brace-enclosed initializer lists.
I've looked up many sources online, and it seems there is not a lot of information in the topic, but came up to things such as FastArduino: initializer list, and Arduino STL: Initializer list. Giving me hopes that it is actually possible. (I can't use those libraries, because they lack the build system generation tools on the library I am developing, they also lack on customizability, and also as a good learning experience)
I've tried various approaches, but I keep getting the error:
could not convert '{0, 1, 2, 3, 4}' from 'brace-enclosed initializer list' to 'cpstd::vector'.
Here's an example of the code that's causing the issue:
cpstd::vector<unsigned char> myVector = {0, 1, 2, 3, 4};
I have already implemented a custom cpstd::initializer_list
and constructor that should handle this, but the issue persists. Is there something specific to the Arduino environment that could be causing this issue?
Additional Information:
I have verified that the
cpstd::initializer_list
is correctly defined and included in my code.The
cpstd::vector
class is included and accessible.I have reviewed my
cpstd::vector
class, and the constructor forcpstd::initializer_list
is correctly implemented.While reading the error log, I noticed that
std::initializer_list
constructor has two parameters, a pointer to the data, and also a variable telling the length of the braced enclosed list, however I noticed that the list ends up casting to a single parameter, hence having no possible constructor to call
What could be causing this issue, and how can I resolve it to initialize cpstd::vector
using brace-enclosed initializer lists in the Arduino environment? I think the main issue is with boards that have no STL support. (i.e. AVR based boards)
I've came to the conclusion that it will not be possible to create such constructors/operators due to compiler limitations, I think I've tried anything that came to mind. But I don't want to give up on this without knowing if there is a workaround.
edit (removed previous code snippets for clarity, added minimal code example reproducing the error) :
minimal sketch reproducing the error:
#include <Arduino.h>
#include <stddef.h>
namespace cpstd {
template<typename T>
class initializer_list {
public:
using value_type = T;
using reference = const T&;
using const_reference = const T&;
using size_type = size_t;
using const_iterator = const T*;
private:
const_iterator _M_array;
size_type _M_len;
constexpr initializer_list(const T* array, size_type size) noexcept
: _M_array(array), _M_len(size) {}
public:
constexpr initializer_list() noexcept
: _M_array(nullptr), _M_len(0) {}
constexpr size_type size() const noexcept {
return _M_len;
}
constexpr const_iterator begin() const noexcept {
return _M_array;
}
constexpr const_iterator end() const noexcept {
return _M_array + _M_len;
}
};
}
template<typename T>
class exampleClass{
protected:
T buffer[10];
size_t len;
public:
exampleClass(): len(0) {}
exampleClass(cpstd::initializer_list<T> il){
len = il.size();
for(size_t i = 0; i < len; i++){
buffer[i] = *(il.begin()+i);
}
}
exampleClass& operator=(cpstd::initializer_list<T> il){
len = il.size();
for(auto i = il.begin(); i < il.end(); i++){
buffer[i] = *i;
}
}
size_t size() const {return len;}
T data(size_t x) const {return buffer[x];}
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
exampleClass<uint8_t> myObject = {0,1,2,3};
Serial.println(myObject.size());
Serial.println(myObject.data(0));
Serial.println(myObject.data(1));
Serial.println(myObject.data(2));
Serial.println(myObject.data(3));
}
void loop() {
// put your main code here, to run repeatedly:
}
cpstd::vector<unsigned char> myVector{0, 1, 2, 3, 4};
(without the=
)?