0

I have a piece of code that uses "unique_ptr", but the Arduino environment does now know about:

#include <memory> 

So i went to C++ reference https://en.cppreference.com/w/cpp/memory/unique_ptr

Defined in header "memory"

template<

    class T,
    class Deleter = std::default_delete<T>
> class unique_ptr;

template <

    class T,
    class Deleter
> class unique_ptr<T[], Deleter>;

Figured that it doesn't take much to "implement".

My H file looks like this:

class state_deleter {  // a deleter class with state
  int count_;
public:
  state_deleter() : count_(0) {}
  template <class T>
  void operator()(T* p) {
    Serial.prinln("[deleted #");
    delete p;
  }
};


template <
    class T,
    class Deleter
> class unique_ptr<T[], Deleter>;



template <class T> class Circular_Buffer {
private:
  unique_ptr<T[],state_deleter> buffer; 
  // i left non relevant code out
};

More info here: https://cplusplus.com/reference/memory/unique_ptr/ Manages the storage of a pointer, providing a limited garbage-collection facility, with little to no overhead over built-in pointers (depending on the deleter used). https://cplusplus.com/ref...nique_ptr/get_deleter/

So i added the above from CPP reference to the piece of code and by adding the "deleter", i removed error messages. But still one remains:

'unique_ptr' is not a class template

I have implemented pieces of code from CPP reference before if i don't had access to it or needed a slightly adapted version and it worked.

Can it be done?

What makes this a "template class" a smart pointer?

class T,
class Deleter

not much too see here in this class definition.

NaturalDemon
  • 934
  • 1
  • 9
  • 21
  • If your code is getting an error, can we get a [mre] of said code and the full error message? – NathanOliver Jan 16 '23 at 16:21
  • @NathanOliver, this is where you can find the code: https://gist.github.com/edwintcloud/d547a4f9ccaf7245b06f0e8782acefaa, the problem is that the Arduino IDE says "unique_ptr" it's not a "template class". – NaturalDemon Jan 16 '23 at 16:36
  • "Defined in header "memory"" thats a declaration not a definition. It does take a bit more to define it. – 463035818_is_not_an_ai Jan 16 '23 at 16:51
  • We don't use external links (well we do but the question has to stand on its own as well). However, if I put the code you did post in a compiler, it throws syntax errors back. If I delete the redundant T and Deleter then it no longer rejects it. – Kenny Ostrom Jan 16 '23 at 16:52
  • @KennyOstrom OP writes that on arduino they do not have ``. – 463035818_is_not_an_ai Jan 16 '23 at 16:54
  • 1
    You say you cannot use `` and tried to implement a unique_ptr, but the code in the github link is using `` and `std::unique_ptr`. In any case a [mcve] should be in the question – 463035818_is_not_an_ai Jan 16 '23 at 16:55
  • scroll down a bit, there's a code block, that has syntax errors which go away if you make it look more like the above excerpt from – Kenny Ostrom Jan 16 '23 at 16:57
  • 1
    you will not find the full definition of `std::unique_ptr` on cppreference. Documentation (and specification in the standard) is about the interface, implementation is up to the implementation of the standard library. – 463035818_is_not_an_ai Jan 16 '23 at 16:57
  • Not directly directed to the question, but you should never, ***never*** [`new`/`malloc` on any kind of microcontroller](https://stackoverflow.com/questions/1725923/how-bad-is-it-to-use-dynamic-datastuctures-on-an-embedded-system). Furthermore, there is no reason to use dynamic allocation in the code you are showing. – Erel Jan 16 '23 at 19:29
  • The "canonical" approach on embedded systems which *really* need to allocate dynamically is to claim a chunk of memory with a `static std::uint8_t buffer[its_size_in_bytes]` and perform `placement new` in it with an `std::allocator`-like structure. But again, I don't believe you need to allocate anything dynamically here. – Erel Jan 16 '23 at 19:37
  • @463035818_is_not_a_number, i found it here: https://gcc.gnu.org/onlinedocs/gcc-5.2.0/libstdc++/api/a01334_source.html, unique_ptr.h – NaturalDemon Jan 17 '23 at 18:49
  • @Erel, just wanna give it a test, i use a Microchip Fubarino and i have 128KB ram, PIC32MX270F256D. – NaturalDemon Jan 17 '23 at 18:51
  • @Erel, yes you can use "new, malloc, etc" on a MCU, the code of a SSD1306 display i have does this as wel, as long as you keep track of objects and destroy objects after use correctly, after all even Microchip has a C++ compiler for their 32 it chips. – NaturalDemon Jan 17 '23 at 19:15
  • I never said you *cannot*. I said you *should not*. The Standard mandates to provide the keyword `new`, so of course it is present in the compiler. This is not only a question of keeping track of what you allocate. Dynamic allocation [brings a lot of problems as described in the post I linked in my first comment](https://stackoverflow.com/questions/1725923/how-bad-is-it-to-use-dynamic-datastuctures-on-an-embedded-system). And as a side note, just because you saw some random code on the internet does not mean that this code follows reasonable programming practices ;) – Erel Jan 17 '23 at 19:44
  • Other references [here](https://electronics.stackexchange.com/questions/171257/realloc-wasting-lots-of-space-in-my-mcu/171581#171581), [here](https://stackoverflow.com/questions/53922828/memory-allocation-in-embedded-systems) and [there](https://stackoverflow.com/questions/21370410/why-shouldnt-we-have-dynamic-allocated-memory-with-different-size-in-embedded-s). I suggest you take a look at these and reconsider to use `new` to do anything but `placement new`. – Erel Jan 17 '23 at 19:49
  • @Erel, changed back to normal array pointer, instead of "unique_ptr", it would take me days to reverse engineer the code from other people, wel i got working for single objects, but not for arrays, but thnx for the effort. – NaturalDemon Jan 17 '23 at 22:39

1 Answers1

0

Found Header files with the declaration of "Unique_ptr" here:

https://github.com/zouxiaohang/TinySTL/blob/master/TinySTL/Memory.h

and

https://github.com/microsoft/STL/blob/main/stl/inc/memory

NaturalDemon
  • 934
  • 1
  • 9
  • 21