std::is_convertible
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <type_traits> 中定义
|
||
template< class From, class To > struct is_convertible; |
(C++11 起) | |
如果类型的假想的rvalue
From
可用于在返回语句中的函数返回To
,即,如果它可以被转换To
使用implicit conversion,提供部件恒定value
等于true。否则value
是false.原文:
If an imaginary rvalue of type
From
can used in the return statement of a function returning To
, that is, if it can be converted to To
using implicit conversion, provides the member constant value
equal to true. Otherwise value
is false.目录 |
Inherited from std::integral_constant
Member constants
value [静态的]</div></div>
|
true如果 From is convertible to To ,false其他方式 原文: true if From is convertible to To , false otherwise (公共静态成员常量) |
Member functions
operator bool |
转换的对象bool,返回 value 原文: converts the object to bool, returns value (公共成员函数) |
Member types
类型
|
Definition |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
</div>
[编辑] 注释
引用类型的,无效的类型,数组类型和函数类型,给出了明确定义的结果.
原文:
Gives well-defined results for reference types, void types, array types, and function types.
[编辑] 示例
#include <iostream> #include <type_traits> int main() { class A {}; class B: public A {}; class C {}; bool b2a = std::is_convertible<B*, A*>::value; bool a2b = std::is_convertible<A*, B*>::value; bool b2c = std::is_convertible<B*, C*>::value; std::cout << std::boolalpha; std::cout << b2a << '\n'; std::cout << a2b << '\n'; std::cout << b2c << '\n'; }
输出:
true false false