- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
API 使用void*
来存储未类型化的指针偏移量。这有点老套,但没关系。
为了表达我的偏移算法,我尝试做这样的事情
int main ()
{
void * foo;
foo = static_cast <int *> (nullptr) + 100;
static_cast <int * &> (foo) += 100;
}
最后一行编译失败(gcc)
x.cpp:7:28: error: invalid static_cast from type ‘void*’ to type ‘int*&’
修复很简单:
foo = static_cast <int *> (foo) + 100;
但为什么不允许第一个呢?
在您回答“因为标准这么说”之前,为什么标准这么说?第一种方法有什么危险吗?或者这只是一个疏忽?
最佳答案
出于与int i; static_cast<long &>(l) = 3L;
相同的原因,不允许这样做不允许。
当然,在很多实现中(int
和 long
具有相同的大小、表示和对齐方式),它可以工作。但是强制转换有效的规则对于所有实现都是基本相同的,显然这在 int
的平台上永远行不通。和 long
具有不同的大小,这意味着不可能允许在这些平台上访问一个作为另一个。
从历史上看,有实现 void *
和 int *
有不同的表示。
后来,在标准声明访问 void *
之后就好像它是一个 int *
是无效的,实现也开始基于有效程序不这样做的假设进行优化:
void *f (void **ppv, int **ppi) {
void *result = *ppv;
*ppi = nullptr;
return result;
}
允许实现对此进行优化
void *f (void **ppv, int **ppi) {
*ppi = nullptr;
return *ppv;
}
而这样的优化,当它们减少代码大小或提高效率时,如今已司空见惯。如果f
被允许称为 void *pv = &pv; f (pv, &static_cast<int*&>(pv));
,此优化将无效。因为这种优化已被证明是有用的,所以规则不太可能改变。
关于c++ - 为什么我可以将 void* static_cast 转换为 int* 但不能转换为 int*&?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855226/
对引用 static_cast 的引用是否与指向指针 static_cast 的指针具有相同的运行时间成本? 例如 class B; class A: public class B; A obj; A
在下面的示例中,编译器接受 static_cast 向下转换,导致未定义的行为,而我认为 static_cast 完全是为了安全(C 风格转换无法提供) . #include class Base
在以下代码中(取自有效的 C++): class A { .... char& operator[](std::size_t position) // now just cal
我正在尝试理解 Pybind11 docs here 中使用的静态转换.具体来说,他们使用语法 static_cast(&Pet::set) 因为在我努力解释并应用到我自己的代码之前我还没有见过这种语
本题不涉及多态,即不涉及虚方法,不涉及虚基类。以防万一,我的案子不涉及这些。 假设我有一个类 Derived它有一个明确的可访问父类型 Base ,没有多态性(没有虚方法,没有虚基类),但可能涉及间接
我看到有人建议使用 static_cast(static_cast(p))而不是重新解释类型转换。 我不明白为什么这样更好,谁能解释一下? 为了便于讨论,这里有一个需要 reinterpret_cas
如果我执行以下操作,一切正常: char* cp = "abc"; void* vp = NULL; vp = static_cast(cp);//ok cp = static_cast(vp);//
让我们有一个名为 Y 的重载函数: void Y(int& lvalue) { cout void f(T&& x) { Y( static_cast(x) ); // Using sta
当你动态分配了一个 char * 类型的缓冲区并想将它转换为特定类型时,你是否应该使用类似的东西 reinterpret_cast(char *) 或者类似的东西 static_cast(static
这是来自 std::enable_if 教程中的示例。 这里有更多的上下文: // handle signed types template auto incr1(Int& target, Int a
我正在阅读 Scott Meyers 的 Effective C++ 3rd。 在第 3 项中: Use const whenever possible. In order to use const
我是 C++ 编程的初学者...我正在练习,遇到了这个问题...这里我尝试在复合运算符上使用 static_cast...我实际上正在尝试除两个整数并得到双倍的答案...这是代码: #include
如果我有两个类: 1) 员工 2) 工程师是派生 从员工 3) 经理是派生 从员工 我被告知(并自己尝试过)以下内容无法编译: Employee employee("John Smith"); Eng
我正在尝试实现一类数值 vector 。我正在使用类似于 STL vector 的 Qt 模板 QVector typedef float ArithmeticF; typedef QVector V
是否使用 static_cast 将 const unsigned char& 转换为 const unsigned long long& 已定义? constexpr unsigned char a
#include class A { public: A() { std::cout (a_obj); // This isn't safe. b_obj
我在我们的生产环境中遇到了以下代码结构(但是大大简化了)。 #include typedef struct { char entry[10]; } inn_struct; typedef s
我有以下两个类: class B; class A { public: A(); operator B() const; }; class B {
我有以下代码片段 class base { public: virtual void MyMethod() { std::cout (b); d->MyMeth
这是一个 discussion on stackoverflow四种显式转换中的一种。但我在投票最多的答案中遇到了一个问题。 引用自投票最多的 wiki 答案: static_cast can als
我是一名优秀的程序员,十分优秀!