jnk0le / Ring-Buffer

simple C++11 ring buffer implementation, allocated and evaluated at compile time
MIT License
380 stars 64 forks source link

const consumerClear producerClear compile errors #11

Closed mgood7123 closed 3 years ago

mgood7123 commented 3 years ago

having const { on these functions produces an incorrect function resolution of <index_t, false> for store

/Users/smallville7123/Desktop/AAudioTrack/AAudioTrack2/src/main/cpp/ardour/Backends/../AudioEngine/../../smallville7123/plugins/../../ringbuffer/ringbuffer.hpp:62:10: error: no matching member function for call to 'store'
                                  tail.store(head.load(std::memory_order_relaxed), std::memory_order_relaxed);
                                  ~~~~~^~~~~
  /Users/smallville7123/Desktop/AAudioTrack/AAudioTrack2/src/main/cpp/ardour/Backends/../AudioEngine/../../smallville7123/plugins/../PianoRoll.h:35:24: note: in instantiation of member function 'jnk0le::Ringbuffer<std::__ndk1::pair<unsigned long long, bool>, 1048576, false, 0, unsigned int>::consumerClear' requested here
          this->noteData.consumerClear();
                         ^
  /Users/smallville7123/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1/atomic:1473:10: note: candidate function not viable: no known conversion from 'const std::atomic<unsigned int>' to 'volatile std::__ndk1::__atomic_base<unsigned int, false>' for object argument
      void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
           ^
  /Users/smallville7123/Library/Android/sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1/atomic:1477:10: note: candidate function not viable: no known conversion from 'const std::atomic<unsigned int>' to 'std::__ndk1::__atomic_base<unsigned int, false>' for object argument
      void store(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT
           ^
  1 warning and 1 error generated.

removing const resolves this and allows the store to be resolved correctly

            /*!
             * \brief Clear buffer from producer side
             */
            void producerClear(void) {
                //this may fail
                head.store(tail.load(std::memory_order_relaxed), std::memory_order_relaxed);
            }

            /*!
             * \brief Clear buffer from consumer side
             */
            void consumerClear(void) {
                tail.store(head.load(std::memory_order_relaxed), std::memory_order_relaxed);
            }
jnk0le commented 3 years ago

right, those are modifying the class members.