Skip to main content

I'm working with a custom C++ library, CPSTL, for Arduino, which includes a cpstd::vectorcpstd::vector class that is designed to work with cpstd::initializer_listcpstd::initializer_list, cpstd::initializer_listcpstd::initializer_list is supposed to mimic std::initializer_liststd::initializer_list.

However, I'm having trouble initializing a cpstd::vectorcpstd::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 and also as a good learning experience)

could not convert '{0, 1, 2, 3, 4}' from 'brace-enclosed initializer list' to 'cpstd::vector<unsigned char>'.

could not convert '{0, 1, 2, 3, 4}' from 'brace-enclosed initializer list' to 'cpstd::vector'.

cpstd::vector<unsigned char> myVector = {0, 1, 2, 3, 4};
    cpstd::vector<unsigned char> myVector = {0, 1, 2, 3, 4};

I have already implemented a custom cpstd::initializer_listcpstd::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?

  • I have verified that the cpstd::initializer_listcpstd::initializer_list is correctly defined and included in my code.

  • The cpstd::vectorcpstd::vector class is included and accessible.

  • I have reviewed my cpstd::vectorcpstd::vector class, and the constructor for cpstd::initializer_listcpstd::initializer_list is correctly implemented.

  • While reading the error log, I noticed that std::initializer_liststd::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::vectorcpstd::vector using brace-enclosed initializer lists in the Arduino environment? I think the maainmain issue is with boards that have no STL support. (i.e. AVR based boards)

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)

could not convert '{0, 1, 2, 3, 4}' from 'brace-enclosed initializer list' to 'cpstd::vector<unsigned char>'.
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?

  • 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 for cpstd::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 maain issue is with boards that have no STL support. (i.e. AVR based boards)

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)

could not convert '{0, 1, 2, 3, 4}' from 'brace-enclosed initializer list' to 'cpstd::vector'.

    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?

  • 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 for cpstd::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)

formatted error listing
Source Link
jsotola
  • 1.5k
  • 2
  • 12
  • 20

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'."

could not convert '{0, 1, 2, 3, 4}' from 'brace-enclosed initializer list' to 'cpstd::vector<unsigned char>'.

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'."

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<unsigned char>'.
deleted 2 characters in body
Source Link
#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(size_tauto i = 0;il.begin(); i < len;il.end(); i++){
        buffer[i] = *(il.begin()+i);*i;
      }      
    }
     

    size_t size() const {return len;}
    T*T data(size_t x) const {return len;buffer[x];}
};
 
exampleClass<uint8_t> myObject = {0,1,2}; // <-- Compilation error: could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'exampleClass<unsigned char>'

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)[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:

}
#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(size_t i = 0; i < len; i++){
        buffer[i] = *(il.begin()+i);
      }      
    }
    
    size_t size() const {return len;}
    T* data() const {return len;}
};
 
exampleClass<uint8_t> myObject = {0,1,2}; // <-- Compilation error: could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'exampleClass<unsigned char>'

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.println(myObject.size());
  Serial.println(myObject.data()[0]);
}

void loop() {
  // put your main code here, to run repeatedly:

}
#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:

}
deleted 2 characters in body
Source Link
Loading
added 485 characters in body
Source Link
Loading
deleted 2 characters in body
Source Link
Loading
deleted 262 characters in body
Source Link
Loading
edited title
Link
Loading
added 849 characters in body
Source Link
Loading
added 176 characters in body
Source Link
Loading
added 176 characters in body
Source Link
Loading
Source Link
Loading