Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Compile time creation of class member stl container (const std::array) filled with elements.
#include <algorithm> // std:find_if #include <vector> // std::vector #include <set> #include <iostream> #include <memory> #include <unordered_set> #include <functional> #include <map> #include <unordered_map> #include <forward_list> #include <cassert> #include <deque> #include <iomanip> #include <chrono> #include <math.h> #include <regex> #include <array> template<class T, size_t MaxIndex> class BaseIndex { public: virtual ~BaseIndex() = default; explicit BaseIndex(const size_t index) : _maxIndex(MaxIndex), _currentIndex(index) {} virtual void DoSmth() = 0; friend std::ostream & operator << (std::ostream & ss, BaseIndex & base_index) { ss << "Max: " << base_index._maxIndex << ". Current: " << base_index._currentIndex << std::endl; return ss; } protected: const size_t _maxIndex; size_t _currentIndex{ 0u }; }; template<size_t MaxIndex> class IndexStr : public BaseIndex<std::string, MaxIndex> { public: explicit IndexStr(const size_t index) :BaseIndex(index) {} void DoSmth() override { std::cout << IndexStr::_maxIndex; } }; template<size_t MaxIndex> class IndexInt :public BaseIndex<int, MaxIndex> { public: explicit IndexInt(const size_t index) :BaseIndex(index) {} void DoSmth() override { std::cout << IndexInt::_maxIndex; } }; class Manager { private: static const size_t _startStrIndex{ 11u }; static const size_t _startIntIndex{ 10060u }; static const size_t _maxStrIndexes{ 5 }; static const size_t _maxIntIndexes{ 120 }; public: template <class T, size_t Size> using _container = const std::array<T, Size>; template <class T> using _ptr = std::shared_ptr<T>; // is it possible to combine this 2 types and use _maxStrIndexes only once? using _limited_str_ = IndexStr<_maxStrIndexes>; using _limited_str_ptr = _ptr<_limited_str_>; using _limited_str_ptr_container = _container<_limited_str_ptr, _maxStrIndexes>; // const std::array<std::shared_ptr<IndexStr<_maxStrIndexes>>, _maxStrIndexes> _strIndexes _limited_str_ptr_container _strIndexes { // _startStrIndex // create indexes ??? // std::integer_sequence ??? std::make_shared<_limited_str_>(_startStrIndex), // 11 = _startStrIndex std::make_shared<_limited_str_>(12), std::make_shared<_limited_str_>(13), std::make_shared<_limited_str_>(14), std::make_shared<_limited_str_>(_startStrIndex + _maxStrIndexes - 1) }; // is it possible to combine this 2 types and use _maxIntIndexes only once? using _limited_int_ = IndexInt<_maxIntIndexes>; using _limited_int_ptr = _ptr<_limited_int_>; using _limited_int_ptr_container = _container<_limited_int_ptr, _maxIntIndexes>; _limited_int_ptr_container _intIndexes { // _startIntIndex // create indexes??? std::make_shared<_limited_int_>(_startIntIndex), // 10060u _startStrIndex //... std::make_shared<_limited_int_>(_startIntIndex + _maxIntIndexes -1) }; }; template <class T, size_t Size > void Process(const std::array<std::shared_ptr<T>, Size> _array) { for (auto &&baseindex : _array) std::cout << *baseindex; } int main() { Manager m; Process(m._strIndexes); //Process(m._intIndexes); return 0; }
run
|
edit
|
history
|
help
0
MSVC ignoring unknown attributes
operator new / delete
What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?
rvalue lifetime
C++ move/copy constructor and assignment demo
Sorting algorithm comparison
Policy class partial specialization inheritance
bitfields_msvc
Guess a number in c++
Not Common Numbers