C ++部分概念ID:显式模板规范顺序/第一个参数的特殊状态的原因是什么? [英] C++ partial concept id: What is the reason for the explicit template specification order / special status of first argument?

查看:39
本文介绍了C ++部分概念ID:显式模板规范顺序/第一个参数的特殊状态的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始尝试概念的C ++ 20功能,当我意识到可以部分显式提供概念的模板参数时,我感到非常高兴.我阅读了cppreference文章,但没有找到那里提到的内容.

但是后来我意识到了一个奇怪的事情:模板参数的指定顺序与我所期望的相反.提供一个显式模板参数时,它将替换模板列表中的第二个模板:

  #include< concepts>#include< type_traits>///为了推断是否某物而提出的概念.是其他的基础模板< typename Impl,typename Base>//XXX:此处Impl和Base的顺序不是概念实现= std :: is_base_of_v< std :: remove_reference_t< Base> ;,//我所期望的.std :: remove_reference_t< Impl> ;;///示例基类implstruct BaseExample {};///BaseExample的实现struct ImplExample:BaseExample {};///应用了该概念的函数模板< Implements< BaseExample> ... Baes>无效f(Baes&& ...){}//}((void)b,...);}int main(){(void)std :: is_base_of_v< BaseExample,std :: remove_reference_t< ImplExample&> ;;;//<真的(void)std :: is_base_of_v< BaseExample,std :: remove_reference_t< ImplExample&> ;;;//<真的f(ImplExample {},ImplExample {});} 

从我的角度来看,部分提供显式模板参数的可能性是有意义的,因为针对类的部分模板规范的参数不适用于此处,并且会使概念更笼统.现在我想知道:

  1. 当标准发布时,是否可能会允许部分显式模板规范?
  2. 规范的顺序可能会保持不变还是这是一个错误?
  3. 我将如何为自己回答这个问题?据我了解,c ++ 20标准目前尚未准备好,我找到了 C ++标准委员会论文,我在其中简要搜索了2020年提出的概念"的标题.是要检查这些文件吗?还是有一个可访问的单一文档,其中包含了作者当前所同意的观点?

可以在此处找到该代码.

修改发布此内容后,当指定了三个模板参数时,我检查了行为.看来我误解了规范的顺序:第一个参数自由持有"以包含要检查的参数,而显式的规范以第二个参数开头.可以在此处看到.即使我弄清楚了规范顺序背后的原因,但我对以上问题的答案仍会非常感兴趣.

解决方案

是的, partial-concept-id 当然是C ++ 20.第一个参数的特殊状态令人惊讶,但允许类似 std :: constructible_from的情况 声明为

  template< class T,class ... Args>概念Constructible_from =…; 

std :: constructible_from< int,int> type-constraint type-constraint ,它要求引入的任何内容都可以从两个 int 参数.但是,它也可以是表达式,在这种情况下,它报告是否可以从 int int >(扰流器: true ),但是无论参数顺序如何,都存在潜在的混乱.

如果必须在末尾加上 T ,将无法使用这样的概念:仅模板参数推导或默认模板参数可以为模板参数提供超出参数包装,但这两个都不适用.

在您链接的论文网站上发布的每封邮件都包含标准的最新草案,备用邮件包含有关采用哪些论文的注释.或者,您也可以访问草稿存储库(至少在您乐于阅读LaTeX的情况下).

I started experimenting with the C++20 feature of concepts and was very pleased when I realized that it is possible to partially explicitly provide template arguments for concepts. I read the cppreference article and did not find that mentioned there.

But then I realized something strange: the order of specification of template arguments is reversed to what I would have expected. When providing one explicite template argument, it replaces the second template in the template list:

#include <concepts>
#include <type_traits>

/// Concept in order to deduce if sth. is base of sth else
template <typename Impl, typename Base>             //XXX: here the order of Impl and Base are not 
concept Implements = std::is_base_of_v<std::remove_reference_t<Base>, // what I would've expected.
                                       std::remove_reference_t<Impl>>;
/// Example Base class impl
struct BaseExample {};
/// Implementation of BaseExample
struct ImplExample : BaseExample {};

/// Function in which the concept is applied
template <Implements<BaseExample>... Baes> void f(Baes &&... ) {}//} ((void)b, ...); }

int main() {
  (void) std::is_base_of_v<BaseExample, std::remove_reference_t<ImplExample &&>>; //< true
  (void) std::is_base_of_v<BaseExample, std::remove_reference_t<ImplExample&>>;  //< true
  f(ImplExample{}, ImplExample{});
}

From my point of view the possibility to partially provide explicit template arguments makes sense, as the argument against partial template specification for classes do not apply here and make concepts more general. Now I wonder:

  1. Will partial explicit template specifications (likely) be allowed when the standard is released?
  2. Will this order of specifications likely stay the same or is this a bug?
  3. How would I answer this question for myself? From what I understand the c++20 standard is not ready by now and I found a list of C++ Standard Committee Papers, of which I briefly searched the headlines of the ones proposed in 2020 for 'concept'. Is checking these papers the way to go, or is there an accessible single document which combines the points the authors currently agreed upon?

The code can be found here.

edit After posting this I checked the behavior when three template arguments are specified. It looks like I misinterpreted the specification order: The first argument is 'held free' to contain the argument to be checked, and the explicit specifications start with the second argument. This can be seen here. Even though I figured out the reasoning behind the order of specification I would be very interested in the answers to questions above.

解决方案

Yes, partial-concept-ids are surely a C++20 thing. The special status of the first argument, while surprising, allows cases like std::constructible_from which is declared as

template<class T,class ...Args>
concept constructible_from=…;

std::constructible_from<int,int> is a type-constraint that requires that whatever it introduces be constructible from two int arguments. However, it can also be an expression, in which case it reports whether an int can be constructed from an int (spoilers: true), but that potential confusion exists regardless of the argument order.

If T had to go at the end, there would be no way of using such a concept: only template argument deduction or default template arguments can supply values for a template parameter beyond a parameter pack, and neither of those applies here.

Every mailing posted at the papers site you linked includes the latest draft of the standard, and alternate mailings include annotations as to what papers were adopted. Or you can just visit the draft’s repository (at least if you’re happy reading LaTeX).

这篇关于C ++部分概念ID:显式模板规范顺序/第一个参数的特殊状态的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆