这是创建const / non-const类的最简单方式吗?

huangapple go评论66阅读模式
英文:

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&lt;const int&gt;.

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&lt;typename T, bool b&gt;
		struct TFactory;

		template&lt;typename T&gt;
		struct TFactory&lt;T, false&gt;{
			using type	= const T;
			using type_void	= const void *;
		};

		template&lt;typename T&gt;
		struct TFactory&lt;T, true&gt;{
			using type	= T;
			using type_void	= void *;
		};

	}

	template&lt;typename T_, bool M_ = false&gt;
	class MyArray{
		using MyTFactory	= my_array_impl_::TFactory&lt;T_, M_&gt;;

		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&lt;T *&gt;(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&amp;operator[](size_t i) const{
			return ptr_[i];
		}

		constexpr auto&amp;operator[](size_t i){
			return ptr_[i];
		}
	};

	template&lt;typename T&gt;
	using MyArrayMutable = MyArray&lt;T, true&gt;;

答案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&lt;typename T&gt;
		using TFactory		= std::conditional_t&lt;Mutable, T, const T&gt;;

		using T			= TFactory&lt;T_		&gt;;
		using Void		= TFactory&lt;void	*	&gt;;

huangapple
  • 本文由 发表于 2023年5月11日 20:07:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定