是否可以使用类型traits来检查类型是否为容器? [英] Is it possible to use type traits to check whether a type is a container?

查看:171
本文介绍了是否可以使用类型traits来检查类型是否为容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用C ++ Type Traits来检查类型是否是类似STL的容器?我已经知道了GCC的内置 __ is_class ,但我想是如果可能的话更具体一点。

Can I use C++ Type Traits to check if a type is an STL-like container? I already know of GCC's builtin __is_class but I would like to be a bit more specific if possible.

推荐答案

您可以创建自己的traits类来检查集装箱接口的类型。这将涉及验证,某些相关类型(如集装箱:迭代)存在并验证某些表达式(如 container.empty())是有效的(即,他们编译没有错误)。各种SFINAE技术可用于构建其性状检查嵌套类型类和验证表达式。

You could build your own traits classes to check a type for the Container interface. This would involve validating that certain associated types (such as container::iterator) exist and validating that certain expressions (such as container.empty()) are valid (i.e., they compile without error). Various SFINAE techniques can be used to build traits classes which check for nested types and validate expressions.

SGI公司的页面详细规定了相关的类型和有效的表达式类型的哪个型号的容器概念必须提供。最新的ISO C ++标准的文档可能会提供更权威的来源,因为SGI的网页是很老。

SGI's page specifies in detail the associated types and valid expressions that types which model the Container "concept" must provide. The most recent ISO C++ standard document would probably provide a more authoritative source since the SGI page is pretty old.

当然,traits类无法验证表达式的语义如 container.empty();他们只能检查表达式是否合法。有些人的提出扩展语言,允许程序员断言语义属性的表达式,这将解决这个限制。

Of course, traits classes can't validate the semantics of expressions like container.empty(); they can only check that the expressions are legal. Some have proposed extending the language to allow the programmer to assert the semantic properties of expressions, which would address this limitation.

这篇关于是否可以使用类型traits来检查类型是否为容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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