什么是℃之间++ 0x中的概念和Boost概念检查库(BCCL)有什么区别? [英] What's the difference between C++0x concepts and The Boost Concept Check Library (BCCL)?

查看:192
本文介绍了什么是℃之间++ 0x中的概念和Boost概念检查库(BCCL)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概念并没有做出的C ++ 0x标准,但仍有提升提供的 Boost概念检查库(BCCL)。我想这BCCL不涵盖,是为了进的C ++ 0x标准的一切。 BCCL和提议的C ++ 0x的解决方案之间的区别是什么?

Concepts didn't make the C++0x standard, but Boost still provides The Boost Concept Check Library (BCCL). I guess that BCCL doesn't cover everything that was meant to into the C++0x standard. What is the difference between BCCL and the proposed C++0x solution?

推荐答案

的概念,这些手动解决方案的一个很大的区别在于概念允许模板的定义来进行类型检查没有做什么特别的事情。这个概念检查库仅允许的使用的它是类型检查。例如:

Checking the template definition

A big difference of concepts to these manual solutions is that concepts allow the definition of a template to be type-checked without doing anything special. The concept check library allows only the use of it to be type checked. Example:

template<typename InputIterator>
int distance(InputIterator a, InputIterator b) 
{ return b - a; }

您现在可以撒上概念检查和特质,模板,但编写模板之后,你将永远不会得到一个错误 - 因为标准允许编译器编译推迟模板,直到实例化。对于检查,你必须写原型类,其中包含正是通过接口所需的操作,然后实例化它们人为地。

You may now sprinkle that template with concept checks and traits, but you will never get an error after writing that template - because the Standard allows the compiler to delay compiling the template until instantiation. For checking, you have to write "archetype" classes, which contain exactly those operations that are required by the interface and then instantiate them artificially.

阅读BCCL的文档,我发现它已经包含了像缺省构造共同的原型。但是,如果你写你自己的概念,你必须也提供自己的原型,这是不容易(你必须要找到完全相同的最小功能类型必须提供)。例如,如果你的原型包含运营商 - ,然后使用(不正确)的原型模板的测试会成功,虽然概念并不需要这样的运营商。

Reading the documentation of BCCL, i found it already includes the common archetypes like "default constructible". But if you write your own concepts, you will have to also provide your own archetypes, which isn't easy (you have to find exactly the minimal functionality a type has to provide). For example, if your archetype contains a operator-, then the test of your template with that (incorrect) archetype will succeed, although the concepts don't require such an operator.

被拒绝的概念,建议您根据上指定的和已暗示(指针类型 T * 在参数中使用将意味着需求会自动创建原型, PointeeType要求为 T ,例如)。你不必在意这个东西 - 当然除了当你的模板定义包含一个类型错误

The rejected concepts proposal creates archetypes for you automatically, based on the requirements that were specified and that were implied (a pointer type T* used in a parameter will imply the PointeeType requirement for T, for example). You don't have to care about this stuff - except of course when your template definition contains a type error.

考虑这个code,用假想的概念检查

Consider this code, using hypothetical concept checks

template<ForwardIterator I>
void f(I a, I b) {
  // loop two times!
  loopOverAToB(a, b);
  loopOverAToB(a, b);
}

该BCCL手册说,语义要求的的检查。仅语法要求和类型进行检查。考虑一个前向迭代:存在,你可以在多通算法使用语义要求。语法检查只是将无法测试这一要求(想想如果一个流迭代器意外将传递查个究竟!)

The BCCL manual says that semantic requirements are not checked. Only syntax requirement and types are checked. Consider a forward iterator: There exists the semantic requirement that you may use it in multi-pass algorithms. Syntax-checking only won't be able to test this requirement (think about what happens if a stream iterator accidentally would pass that check!)

在拒绝了提议,你必须明确提出汽车在概念定义前,使语法检查之后,编译器标志成功。如果没有指定汽车中,然后选择一种有明确定义一个概念图说,它支持的概念。甲流迭代器将因此永远不会采取通过一个ForwardIterator检查。

In the rejected proposal, you had to explicitly put auto in front of concept definitions to make the compiler flag success after syntax-checking. If auto wasn't specified, then a type explicitly had to define a concept map to say it supports that concept. A stream iterator would thus never be taken to pass a ForwardIterator check.

这是另一个特点。一个模板,如

This was another feature. A template such as

template<InputIterator I>
  requires OutputStreamable<I::value_type>
void f(I a, I b) {
  while(a != b) std::cout << *a++ << " ";
}

可用于像下面,如果用户将提供一个概念图,教导编译器如何取消引用的整数,和一个整数因而如何满足InputIterator的概念。

Can be used like the following, if the user would provide a concept map that teaches the compiler how to dereference an integer, and thus how an integer satisfies the InputIterator concept.

f(1, 10);

这是一个基于语言的解决方案的好处,而不能由BCCL永远不会得到解决,我相信。

This is the benefit of a language-based solution, and cannot be solved by BCCL ever, i believe.

在BCCL的快速阅读,我也不能发现任何允许这种情况发生。一个概念匹配失败似乎导致硬编译错误。被拒绝的提议允许以下内容:

On a quick read of BCCL, i can also not spot anything that allows this to happen. A concept matching failure seems to cause a hard compilation error. The rejected proposal allows the following:

template<ForwardIterator I>
I::difference_type distance(I a, I b) {
  I::difference_type d = 0; while(a != b) ++a, ++d;
  return d;
}

template<RandomAccessIterator I>
I::difference_type distance(I a, I b) {
  return b - a;
}

如果一个类型可以使用两个模板来使用,那么第二个模板会被使用,因为它更专业: RandomAccessIterator的细化 ForwardIterator 的概念。

If a type could be used with both templates, then the second template would be used, because it's more specialized: RandomAccessIterator refines the ForwardIterator concept.

这篇关于什么是℃之间++ 0x中的概念和Boost概念检查库(BCCL)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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