Jon*_*ove -3 c++ reinterpret-cast c++11
我想重新声明给定变量的类型,但是不幸的是rerequire_cast <>在这里没有帮助。这行:
reinterpret_cast<std::vector<double>>(std::string("Hello"));
Run Code Online (Sandbox Code Playgroud)
导致以下编译器错误:
invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'std::vector<double>'
Run Code Online (Sandbox Code Playgroud)
有没有其他干净的方法?
笔记:
编辑/某些上下文:此行将是模板化函数的一部分,具有:
reinterpret_cast<T>(std::string())
Run Code Online (Sandbox Code Playgroud)
对于T == std :: string,这是完全可以的,但是不幸的是,编译器还将尝试为T == std :: vector <>实例化(但在运行时从不使用)它。这是C ++ 11,因此没有static_if。
您不能,这样做没有任何意义,编译器告诉您。
reinterpret_cast 无法将一种类型转换为完全不相关的类型。
即使这样做,例如在最终要点中出现的指针黑客行为,该语言的规则也禁止您使用经过这种强制转换的对象。
根本就不要做,因为你做不到。
如果要尝试从字符串(?)构造双精度型向量,请按照业务需求规定的方式编写相应的代码以从字符串中生成双精度型。
类型系统可以为您提供帮助。让它。
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |