检查类型(a)==类型(b)还是类型(a)==类型(b带有删除的constness)

发布于 2025-02-03 04:53:26 字数 1061 浏览 2 评论 0原文

我在元素的向量存储中创建矩阵视图。

有效组合为:

vector value_type矩阵value_type
const(arithmetic)const(arithmetic)
(arithmetic)(arithmetic)const(arithmetic)(arithmetic)
(arithmetic)(arithmetic) (arithmetic)

so,so,一个典型的矩阵构造者可以可以是:

template<std::contiguous_iterator It>
requires(std::is_same_v<typename std::iterator_traits<It>::value_type, value_type> ||
         std::is_same_v<typename std::iterator_traits<It>::value_type, std::remove_const_t<value_type>>)
matrix(It it) requires(H != -1 && view) : elements(std::to_address(it)) {}

我可以使用STD设施更短暂/简短/简短/简短?

我尝试了std :: convertible_to&lt; const double,double&amp;&gt;用于测试并完成工作,但随后我尝试了std :: convertible_to&lt&const&const double,const int,const int&gt; /code>,并且它不会像我想要的那样失败。

I create a matrix view over a vector storage of elements.

Valid combinations are:

vector value_typematrix value_type
const (arithmetic)const (arithmetic)
(arithmetic)const (arithmetic)
(arithmetic)(arithmetic)

So, a typical matrix constructor can be:

template<std::contiguous_iterator It>
requires(std::is_same_v<typename std::iterator_traits<It>::value_type, value_type> ||
         std::is_same_v<typename std::iterator_traits<It>::value_type, std::remove_const_t<value_type>>)
matrix(It it) requires(H != -1 && view) : elements(std::to_address(it)) {}

Can I do this a little bit more brief/short/laconic with an std facility?

I tried std::convertible_to<const double, double&> for testing and it does the job, but then I tried std::convertible_to<const double, const int&> and it does not failed as I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2025-02-10 04:53:26

如果您只想约束其参考类型可以转换为value_type的迭代器类型,则可以通过以下方式重写构造函数。

template<std::contiguous_iterator It>
requires std::is_convertible_v<std::iter_reference_t<It>, value_type>
matrix(It it)
 requires(H != -1 && view)
 : elements(std::to_address(it)) {}

重要的是要注意,要使约束甚至更短,std :: iterator_traits类型特征被std :: iter _**组替换。

If you want only to constraint iterator types whose reference type can be converted into value_type, it is possible to rewrite the constructor in the following way.

template<std::contiguous_iterator It>
requires std::is_convertible_v<std::iter_reference_t<It>, value_type>
matrix(It it)
 requires(H != -1 && view)
 : elements(std::to_address(it)) {}

It is important to note that, to make the constraint even shorter, the std::iterator_traits type trait is replaced by the std::iter_* group of aliases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文