C ++概念相同且可分配 [英] C++ Concepts Same and Assignable

查看:58
本文介绍了C ++概念相同且可分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在尝试C ++概念.我正在尝试以下范围扩展"文档中的定义:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf

Same 的定义和用法使我感到困惑.由于我未知的原因,作者没有给出明确的定义.所以我正在使用:

 模板<类别T,类别U>概念布尔Same(){返回std :: is_same< T,U> :: value;} 

问题在于文档为 Assignable 提供了以下定义:

 模板<类别T,类别U>概念bool Assignable(){返回Common< T,U>()&&require(T& a,U& b){{std :: forward< T(a)= std :: forward< U>(b)}->相同的T&>};} 

它不起作用(在GCC 6.3中):一个简单的 Assignable< int& ;, int&&>()概念检查为我提供了 false (我有确认 Common 部分正常).我必须将 Same< T&> 更改为 T& 才能使其正常工作.在其他一些地方也使用相同的 Same< Type> 检查.

我的问题是:

  • 我对 Same 的定义正确吗?
  • 为什么使用 Same< T&> 代替 T& ?有什么区别?

感谢您的帮助.

解决方案

在周末解决问题之后,我想我自己找到了答案.

Eric Niebler和Casey Carter对 Same 的定义更精确,它支持多个模板参数(不只是两个),但是对于两个参数的情况,我的定义基本上是正确的.

使用->时,类型,目的是可以将方括号中的表达式隐式转换为 Type .使用->时,相同< Type> ,目的是使方括号中的表达式恰好是 Type .因此它们是不同的.

但是,有一个陷阱.约束检查非常复杂,甚至Eric和Casey之类的专家都犯了一个错误,并在N4569中给出了错误的定义.Eric在GitHub上讨论了这个问题:

https://github.com/ericniebler/stl2/issues/330

按照N4569中给出的方式使用时,意味着该表达式应该能够传递给想象的函数模板,例如

 模板< typename U>f(U)需要Same< T& U() 

这不起作用-如果传入的表达式是 T 的左值,则推导的 U T 而不是 T& .解决方案是在 Assignable 中使用 Same< T&&& .这将产生以下想象的功能模板:

 模板< typename U>f(U&&)需要Same< T& U() 

现在一切正常,如果传入的表达式是 T 的左值,则必须将 U 推导出为 T& ./p>

玩概念对我来说是一个好习惯,但是我可能应该更早找到它们的代码.它们在以下GitHub存储库中具有完整的概念集:

https://github.com/CaseyCarter/cmcstl2

对C ++概念感兴趣的人应该调查一下.

I have been experimenting with C++ concepts recently. I am trying the definitions from the following Ranges Extensions document:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf

The definitions and usages of Same are confusing me. For reasons unknown to me, the authors did not give an explicit definition. So I am using:

template <class T, class U>
concept bool Same()
{
  return std::is_same<T, U>::value;
}

The problem is that the document gives the following definition for Assignable:

template <class T, class U>
concept bool Assignable()
{
  return Common<T, U>() && requires(T&& a, U&& b) {
    { std::forward<T>(a) = std::forward<U>(b) } -> Same<T&>;
  };
}

It does not work (under GCC 6.3): a simple Assignable<int&, int&&>() concept check gives me false (I have verified that the Common part is OK). I have to change Same<T&> to T& to make it seemingly work. The same Same<Type> check is used in some other places too.

My questions are:

  • Is my definition of Same correct?
  • Why is Same<T&> used instead of T&? What are the differences?

Thanks for any help.

解决方案

After attacking the problem during the weekend, I think I have found the answer myself.

Eric Niebler and Casey Carter have a more refined definition of Same that supports multiple template arguments (not just two), but my definition should be basically right for the two-argument case.

When using -> Type, the purpose is that the expression in the brackets can be implicitly converted to Type. When using -> Same<Type>, the purpose is that the expression in the brackets is exactly Type. So they are different.

However, there is a gotcha. The constraint check is quite complicated, and even experts like Eric and Casey made a mistake and gave wrong definitions in N4569. Eric discussed the issue on GitHub:

https://github.com/ericniebler/stl2/issues/330

When used the way it was given in N4569, it means the expression should be able to be passed to an imagined function template like

template <typename U>
f(U)
requires Same<T&, U>()

This doesn't work—if the expression passed in is an lvalue of T, the deduced U is T instead of T&. The solution is use Same<T&>&& in Assignable. It will result in the following imagined function template:

template <typename U>
f(U&&)
requires Same<T&, U>()

Now everything is OK—if the expression passed in is an lvalue of T, U has to be deduced as T&.

Playing with concepts is a good practice for me, but I probably should find their code earlier. They have a complete set of concepts in the following GitHub repository:

https://github.com/CaseyCarter/cmcstl2

People interested in C++ concepts should look into it.

这篇关于C ++概念相同且可分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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