英文:
Is this the simplest way to create const / non const class?
问题
I wanted to do a class that works similarly to an array.
然而,大多数我的指针都是const的,即使我使用 MyArray<const int>
也会出现问题。
This is why i "separated" const and non const class.
这就是为什么我“分离”了const和非const类。
However I feel all code inside my_array_impl_ namespace is bit redundant.
然而,我觉得my_array_impl_命名空间中的所有代码都有点冗余。
Is there an easier way to achieve the same with less code? Thanks.
是否有更简单的方法以更少的代码实现相同的功能?谢谢。
namespace my_array_impl_{
在my_array_impl_命名空间中:
template<typename T, bool b>
struct TFactory;
template
struct TFactory<T, false>{
using type = const T;
using type_void = const void *;
};
template
struct TFactory<T, true>{
using type = T;
using type_void = void *;
};
}
template<typename T_, bool M_ = false>
class MyArray{
using MyTFactory = my_array_impl_::TFactory<T_, M_>;
using T = typename MyTFactory::type;
using Void = typename MyTFactory::type_void;
T *ptr_;
size_t size_;
public:
constexpr MyArray(Void ptr, size_t size) :
ptr_(reinterpret_cast<T *>(ptr)),
size_(size / sizeof(T)) {}
explicit
constexpr MyArray(std::string_view s) :
MyArray(s.data(), s.size()) {}
public:
constexpr size_t size() const{
return size_;
}
constexpr const auto &operator[](size_t i) const{
return ptr_[i];
}
constexpr auto &operator[](size_t i){
return ptr_[i];
}
};
template
using MyArrayMutable = MyArray<T, true>;
英文:
I wanted to do a class that works similarly to an array.
However most of my pointers were const, and I had problems even if I use MyArray<const int>
.
This is why i "separated" const and non const class.
However I feel all code inside my_array_impl_
namespace is bit redundant.
Is there easier way to achieve the same with less code? Thanks
namespace my_array_impl_{
template<typename T, bool b>
struct TFactory;
template<typename T>
struct TFactory<T, false>{
using type = const T;
using type_void = const void *;
};
template<typename T>
struct TFactory<T, true>{
using type = T;
using type_void = void *;
};
}
template<typename T_, bool M_ = false>
class MyArray{
using MyTFactory = my_array_impl_::TFactory<T_, M_>;
using T = typename MyTFactory::type;
using Void = typename MyTFactory::type_void;
T *ptr_;
size_t size_;
public:
constexpr MyArray(Void ptr, size_t size) :
ptr_ (reinterpret_cast<T *>(ptr) ),
size_ (size / sizeof(T) ){}
explicit
constexpr MyArray(std::string_view s) :
MyArray(s.data(), s.size()){}
public:
constexpr size_t size() const{
return size_;
}
constexpr const auto&operator[](size_t i) const{
return ptr_[i];
}
constexpr auto&operator[](size_t i){
return ptr_[i];
}
};
template<typename T>
using MyArrayMutable = MyArray<T, true>;
答案1
得分: 1
All code can be simplified with std::conditional
:
template<typename T>
using TFactory = std::conditional_t<Mutable, T, const T>;
using T = TFactory<T_>;
using Void = TFactory<void*>;
英文:
All code can be simplified with std::conditional
:
template<typename T>
using TFactory = std::conditional_t<Mutable, T, const T>;
using T = TFactory<T_ >;
using Void = TFactory<void * >;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论