Simple ring/circular buffer c++ class V3

The Original Post (v2)

As stated in the previous post, i’m looking for feedback on the updated version of the code posted.

A few things specifically I’m looking for feedback on, are:

  • I switched to using the std::aligned_storage as suggested in the previous post, but on the cppreference.com – std::aligned_storage page, in the examples it says // note: needs std::launder as of C++17 above the reinterpret_cast‘s. Would that mean, that the launder function should just wrap the cast, and if so, it states that its needed for c++17, which is what I’m using, but I’d also like the code to be compatible back to c++11. What would be the recommended approach in this situation?

  • I was considering making a private function, something like:

    #include <new> // ... [[nodiscard]] constexpr const T* ptr_to(std::size_t pos) const {     return std::launder(reinterpret_cast<const T*>(&data[pos])); } 

    to remove some of the code duplication, I’m still trying to gain a better understanding of const correctness, would I need a const version and a non-const version of this function, similar to the peek function?

  • Is there any reason to reinterpret_cast to T* rather then const T* within this class?

  • Like I mentioned in a previous point, I’m wanting to target c++17 primarily, but also be backward compatible down to c++11. Language features such as [[nodiscard]] and std::launder are c++17 only, is there a standard way(using macros or otherwise) to accommodate for this?

  • The function void push(const T& item) noexcept takes a const ref and uses std::move internally on it, is that reasonable? or should that only be done to params such as T&& item? Should I just delete the std::move in that function?

CircularBuffer.h

#pragma once  #include <cassert> #include <type_traits>  namespace datastructures {      template<class T, std::size_t N>     class CircularBuffer {          typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];         std::size_t head = 0;         std::size_t tail = 0;         bool isFull = false;       public:         template<typename ...Args>         void emplace_push(Args&&... args) noexcept {             assert(!isFull && "Attempting to push item into full buffer!");              new (&data[head]) T(std::forward<Args>(args)...);              head = ++head % N;             isFull = head == tail;         }          void push(const T& item) noexcept {             assert(!isFull && "Attempting to push item into full buffer!");              new (&data[head]) T(std::move(item));              head = ++head % N;             isFull = head == tail;         }          T pop() noexcept {             assert(!is_empty() && "Attempting to pop item from empty buffer!");              auto ptr = reinterpret_cast<T*>(&data[tail]);             auto result = std::move(*ptr);             ptr->~T();              tail = ++tail % N;             isFull = false;              return result;         }          [[nodiscard]] constexpr T& peek() noexcept {             assert(!is_empty() && "Attempting to peek in empty buffer!");              return *reinterpret_cast<T*>(&data[tail]);         }          [[nodiscard]] constexpr const T& peek() const noexcept {             assert(!is_empty() && "Attempting to peek in empty buffer!");              return *reinterpret_cast<const T*>(&data[tail]);         }          [[nodiscard]] constexpr bool is_empty() const noexcept {             return !isFull && tail == head;         }          [[nodiscard]] constexpr std::size_t get_capacity() const noexcept {             return N;         }          [[nodiscard]] constexpr std::size_t get_size() const noexcept {             if (isFull)                 return N;              if (head >= tail)                 return head - tail;              return N + head - tail;         }     }; } 

expected unqualified-id before ‘class’

I’ve been getting the same error each time and I can’t seem to figure out what’s wrong

It also keeps saying ‘TextChoice does not name a type’ in the .cpp file. I am not the best with classes so any help would be appreciated

.h File:

#define TEXTCHOICE_H  #include <iostream> #include <sstream>  #include "Choice.h"  class TextChoice { private:     int numChoices;     Choice *choices;     std::string text;  public:     TextChoice();     TextChoice(int);      std::string getText();     std::string getChoiceNextText();     int getNumChoices();     int getChosenPath();      void setChoiceNextText(int, std::string);     void setText();     void setNumChoices();     void setChosenPath(); };  #endif // TEXTCHOICE_H 

.cpp File

 TextChoice::TextChoice() {     choices = new Choice[3];     text = "Unfinished";     numChoices = 3; }  TextChoice::TextChoice(int numC) {     choices = new Choice[numC];     text = "Unfinished";     numChoices = numC; }  TextChoice::~TextChoice() {     delete [] choices; } 

Variable from class doesn’t show in editor

this may be another beginner question. I have a simple data holding script and want to pass one variable from it as parameter to a function. But in the editor it only shows me the script name, not the option of any variable. What attribute or change do I need to make to make the variable visible for selection? (Passing the whole object works, but I don’t like the parsing of the object inside the called method and would just to pass the parameter directly) Picture for easier understanding.

enter image description here

Is paladin mount advancement strictly tied to paladin class level?

The SRD has a table on paladin mount advancement that seems to be linked to paladin level. I was wondering how this interacted with multiclassing and prestige classes.

  1. Does the paladin mount advance when the paladin is multiclassing?
  2. Does the paladin mount advance when taking levels in a prestige class?
  3. Does the paladin mount advance when taking levels in the Fist of Raziel class?

If the answer to the questions above is no, would it be irrational to house-rule that prestige classes that work well with the paladin class could also count on the table for paladin mount advancement?

Simple ring/circular buffer c++ class V2

The Original Post (v1)

I’m looking for feedback on the updated version of the code posted at the link above.

#pragma once  #include <memory> #include <cassert> #include <type_traits>  namespace datastructures {      template<class _Ty, size_t _Size>     class CircularBufferv2 {          // uses char as type to prevent default _Ty initialization.         alignas(alignof(_Ty)) char buffer[_Size * sizeof(_Ty)];         size_t head;         size_t tail;         bool isFull;      public:         constexpr CircularBufferv2() noexcept :             buffer{0},             head{0},             tail{0},             isFull{false} {         }          void push(const _Ty& item) noexcept {             assert(!isFull && "Attempting to push item into full buffer!");              new (&buffer[head * sizeof(_Ty)]) _Ty(std::move(item));              head = ++head % _Size;             isFull = head == tail;         }          _Ty pop() noexcept {             assert(!is_empty() && "Attempting to pop item from empty buffer!");              auto location = reinterpret_cast<_Ty*>(&buffer[tail * sizeof(_Ty)]);             auto result = std::move(*location);             std::destroy_at(location);              tail = ++tail % _Size;             isFull = false;              return result;         }          _NODISCARD constexpr _Ty& peek() noexcept {             assert(!is_empty() && "Attempting to peek in empty buffer!");              return *reinterpret_cast<_Ty*>(&buffer[tail * sizeof(_Ty)]);         }          _NODISCARD constexpr const _Ty& peek() const noexcept {             assert(!is_empty() && "Attempting to peek in empty buffer!");              return *reinterpret_cast<_Ty*>(&buffer[tail * sizeof(_Ty)]);         }          _NODISCARD constexpr bool is_empty() const noexcept {             return !isFull && tail == head;         }          _NODISCARD constexpr size_t get_capacity() const noexcept {             return _Size;         }          _NODISCARD constexpr size_t get_size() const noexcept {             if (isFull)                 return _Size;              if (head >= tail)                 return head - tail;              return _Size + head - tail;         }          _NODISCARD _CONSTEXPR17 _Ty* data() noexcept {             return buffer;         }          _NODISCARD _CONSTEXPR17 const _Ty* data() const noexcept {             return buffer;         }     }; } 

I want to take advantage of all of the new features(c++17) while also supporting older compilers(preferably all older compilers but c++11 is probably as old as i’ll actually be compiling for). Any suggestions welcome. (I’m attempting to use this class as an example class to follow when constructing other classes.)

Also, the use of _CONSTEXPR17 on the data functions I was wondering why use the macro vs just a constexpr? (I based the use of macros around the std::array struct, its data function uses _CONSTEXPR17 rather than just constexpr.)

CSS class naming convention

I think every frontend developer knows how BEM works right? If not, that’s okay. Just Google it and you’ll find a LOT of documentation and articles about it. Personally, I love how intuitive and innovative BEM is. For small components, it works like a charm. But as you project requires longer components which happens to require many more DOM tree, you’ll surely need something more.

Let’s supose that you’re developing a full custom theme for an entreprise-level website/blog/ecommerce and you need to make a highly styleable menu. BEM methodology recommend that we style our components based on the following class structure:

.menu .menu__item .menu--blue 

But what if you need a child for that item element? And another child for that one too?

This is where BEM core concepts starts to fall apart and my “methodology” comes in. You can have as many childs and grandchildren as you need. For example:

.header .header-menu .header-menu-links .header-menu-links-item-label .header-menu-links-item-icon 

You may wonder how in the name of God you would add an modifier to that monster. If we were going to do something like this:

// Wrong  .header-menu-links-item-icon .header-menu-links-item-icon--star 

Eventually, it would make our markup unreadable. Instead, I propose that we do it like this:

// Okay  .header-menu-links-item-icon .header-menu-links-item-icon .is-star 

That way, any class name that starts with is- would be a modifier for one of the sibling classes. And for that, you could use inheritance, like this:

.header .is-theme-purple  .header .is-theme-purple .header-menu .header .is-theme-purple .header-menu-links .header .is-theme-purple .header-menu-links-item  .header .is-active .button .is-lg 

Here is an markup example of this methodology:

<div class="menu">     <div class="menu-links">         <div class="menu-links-item is-current">Item 1</div>         <div class="menu-links-item">Item 2</div>         <div class="menu-links-item">Item 3</div>         <div class="menu-links-item">Item 4</div>     </div>     <button class="button is-size-lg is-color-primary is-shadow"></button> </div> 

Alternatively, you can omit is from modifiers turning it into something like button -lg -primary -shadow.

Important: While you can obviously use this concept with pure css, it is highly recommended that you use a css preprocessor like SASS.

PS.: Btw, I would like to hear what you guys think about it 🙂

Do class features that give a flying speed count as magical in relation to falling rules?

There are some classes, like the vengeance paladin’s capstone or the Tempest Cleric’s 17th level Stormborn feature, that grant a flying speed to a creature.

Is this considered ‘magical’ flight or is it mundane flight?

I’m thinking of this with regard to the rules on falling when a creature has their speed reduced to 0 or if knocked prone:

If a flying creature is knocked prone, has its speed reduced to 0, or is otherwise deprived of the ability to move, the creature falls, unless it has the ability to hover or it is being held aloft by magic

What is considered a class feature for a monster for the purpose of Change Shape?

It’s well understood that Spellcasting in a creature’s stat block is a class feature. For the purposes of Change Shape and similar abilities where the term “class features” has mechanical importance, are there any other class features in official monster stat blocks? The wording in Monster Manual implies that such things could exist, but do they, and if so how do we tell?

Example cases:

  • Kobold Scale Sorcerer’s Sorcery Points vs Sorcerer? Would a shape changed Couatl be able to sorcerize its innate spells?
  • Drow Shadowblade’s Shadow Step vs Shadow Monk?
  • Firenewt Warlock’s Imix’s Blessing vs Fiend Warlock’s Dark One’s Blessing?
  • Githzerai Anarch’s Psionic Defense and Unarmed Strike vs Monk? (I don’t think we’re in class feature any more.)
  • NPC Assassin’s Assassinate, Evasion, and Sneak Attack vs Assassin Rogue?