I tried to create a minimal example as it is possible with templates.
There are 2 questions may be connected with each other.
Question 1: Is it possible to create at compile time container class member (for example std::array) filled with elements?
_limited_int_ptr_container _intIndexes
{
// _startIntIndex
// create indexes???
std::make_shared<_limited_int_>(_startIntIndex), // 10060u _startStrIndex
//std::make_shared<_limited_int_>(_startIntIndex + 1),
//std::make_shared<_limited_int_>(_startIntIndex + 2),
//...
std::make_shared<_limited_int_>(_startIntIndex + _maxIntIndexes -1)
};
Question 2: Is it possible to combine several template classes with one general template parameter but only once?
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>;
Full code:
#include <iostream>
#include <memory>
#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;
}
Aucun commentaire:
Enregistrer un commentaire