val*_*ica 14 c++ rtti string-literals c++17
我正在制作一个C ++库,该库在很大程度上依赖于RTTI(到另一种语言的可自定义桥接),并且对字符串文字类型非常困惑。
这是我做的一个简单测试,用于显示问题:
std::cout << typeid(const char*).name() << std::endl; // PKc
std::cout << std::any("").type().name() << std::endl; // PKc
std::cout << typeid("").name() << std::endl; // A1_c
Run Code Online (Sandbox Code Playgroud)
对我来说,它看起来像前两个打印出的类型const char*,但最后一个是数组。
为什么对结果std::any("").type()和typeid("")不同?有没有办法获得第一个行为,即使字符串文字的结果一致(我使用类型标识来调用不同的类型处理程序)?
PS:在Ubuntu 19.04上使用Clang版本8.0.0-3(标签/ RELEASE_800 / final)进行测试。
Hol*_*olt 17
正如其他人提到的那样,字符串文字的类型""是const char[1],例如,C ++中字符串文字的数据类型是什么?。
存储的类型std::any("")是const char*因为您正在使用以下构造函数(http://www.eel.is/c++draft/any.cons#8):
// Effects: Constructs an object of type any that contains an object of
// type std::decay_t<T> direct-initialized with std::forward<T>(value).
template< class T>
any( T&& value );
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Tis是const char(&)[1](字符串文字的类型""),因此std::decay_t<const char(&)[1]>将给您const char*,这就是为什么typeid()of std::any("").type()是of 的类型ID const char*。
| 归档时间: |
|
| 查看次数: |
717 次 |
| 最近记录: |