(Boost) 为什么我们需要泛型类型?
为什么我们需要 Boost 库中指定的泛型类型?模板还不够吗? 例如,如果我想要一个特定类型的容器,我会这样做:
template<class Type>
vector<Type> v;
如果我想指定一个包含所有内容的容器,我只需写:
vector v;
Interpretation for boost::any (http://www.boost.org /doc/libs/1_46_1/doc/html/any/s02.html)
转换可以包含其中之一的类型 许多可能的值类型,例如 int和string,自由转换 他们之间,例如 将 5 解释为“5”,反之亦然。 此类类型在脚本编写中很常见 其他解释语言。 boost::lexical_cast 支持这样的 转换功能。
为什么我们需要像 PHP 这样的脚本语言中的隐式类型?
此外,在 boost::any 示例中,why:
using boost::any_cast;
typedef std::list<boost::any> many;
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
是可以接受的?容器是否使用 boost::any 中实现的运算符=?
any & operator=(const any &);
这使得 boost::any 能够容纳任何类型? boost::any 中定义的运算符 = 解释为:
作用:将rhs的内容复制到 当前实例,丢弃前一个实例 内容,因此新内容是 在类型和值上等同于 rhs 的内容,如果为空 rhs.empty()。
抛出:std::bad_alloc 或任何 副本引起的例外情况 所包含类型的构造函数。 分配满足强 保证异常安全。
http://www.boost.org/doc/libs /1_46_1/doc/html/boost/any.html
why do we need a generic type specified in Boost library? Isn't template enough?
Fore example, if I want a container of a specific type, I would just do:
template<class Type>
vector<Type> v;
If I want to specify a container which contains everything, I simply write:
vector v;
In explanation for boost::any (http://www.boost.org/doc/libs/1_46_1/doc/html/any/s02.html)
Converting types that can hold one of
a number of possible value types, e.g.
int and string, and freely convert
between them, for instance
interpreting 5 as "5" or vice-versa.
Such types are common in scripting and
other interpreted languages.
boost::lexical_cast supports such
conversion functionality.
Why do we need implicit type like in scripting languages such as PHP?
Further more, in the boost::any example, why:
using boost::any_cast;
typedef std::list<boost::any> many;
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
is acceptable? Does the container use the operator = implemented in boost::any?
any & operator=(const any &);
which makes boost::any able to hold any type? Operator = defined in boost::any is explained as:
Effects: Copies content of rhs into
current instance, discarding previous
content, so that the new content is
equivalent in both type and value to
the content of rhs, or empty if
rhs.empty().Throws: std::bad_alloc or any
exceptions arising from the copy
constructor of the contained type.
Assignment satisfies the strong
guarantee of exception safety.
http://www.boost.org/doc/libs/1_46_1/doc/html/boost/any.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为 C++ 没有泛型类型。它具有类型模板,这些模板是为每个替换类型单独编译的。该表达式
是一个语法错误,因为向量不是一个类(它是一个类模板,可以通过给它模板参数来实例化为类)。
C++ 中有一种通用类型,即
void*
,但您有责任记住其中存储的内容(尤其是出于删除目的)。boost::any
是一种类型安全的替代方案,它会记住您存储在那里的内容,并且如果您尝试将其转换为未定义转换的内容,则会给出错误(您仍然需要询问它any_cast
的实际值)。至于
operator=
,是的,它是容器使用的。标准容器通常要求元素类型是默认可构造和可赋值的,这意味着它们需要具有工作复制构造函数和赋值运算符。它们通常不必是默认可构造的,或者仅在使用某些操作时才必须是默认可构造的。Because C++ does not have generic type. It has type templates, which are compiled for each substituted type separately. The expression
is a syntax error, because vector is not a class (it's a class template that can be instantiated to class by giving it template parameters).
There is a somewhat generic type in C++, the
void*
, but then it's your responsibility to remember what you stored in it (esp. for purposes of deletion). Theboost::any
is a typesafe alternative that remembers what you stored there and will give an error if you try to convert it to something it does not have defined conversion to (you still have to ask it for the actual value byany_cast
).As for
operator=
, yes, it is used by the container. The standard containers generally require the element type to be default constructible and assignable, which means they need to have working copy constructor and assignment operator. They generally either don't have to be default constructible or only have to be default constructible if you use certain operations.查看更多极端示例。如果有人这样做,显然他们需要它。
See a more extreme example. If somebody does it, apparently they need it.
因为模板类型解析是编译时的,而
boost::any
类型分辨率是运行时的。
boost::any
是您需要做的事情之一应尽可能避免,但在少数情况下使用它
有道理,你确实需要它——没有其他办法可以做到。
Because template type resolution is compile time, and
boost::any
typeresolution is run-time.
boost::any
is one of those things that youshould avoid as much as possible, but in the few cases where its use is
justified, you really need it—nothing else will do.