jjzjj

c++ - 使用模板输入双关语

我想知道以下代码是否是一种可接受的方法,可以在不违反严格的别名规则的情况下处理类型双关语。我意识到此方法依赖于GCC编译器扩展,因此无需指出这一点。templateinlineoutput_typepunning_cast(constinput_type&input){static_assert(std::is_pod::value,"output_typeforpunning_castmustbePOD");static_assert(std::is_pod::value,"input_typeforpunning_castmustbePOD");static_assert(size

c++ - 什么是双关语,它的目的是什么?

TypepunningAformofpointeraliasingwheretwopointersandrefertothesamelocationinmemorybutrepresentthatlocationasdifferenttypes.Thecompilerwilltreatboth"puns"asunrelatedpointers.Typepunninghasthepotentialtocausedependencyproblemsforanydataaccessedthroughbothpointers.这篇文章想表达什么?如果我使用它或不使用它会怎样?

c++ - 有效类型的 memmove 就地更改(类型双关)

在下面的问题中:What'saproperwayoftype-punningafloattoanintandvice-versa?,结论是从整数位构造double的方法是通过memcpy构造double,反之亦然。很好,找到的pseudo_cast转换方法是:templateinlineTpseudo_cast(constU&x){static_assert(sizeof(T)==sizeof(U));Tto;std::memcpy(&to,&x,sizeof(T));returnto;}我会这样使用它:intmain(){static_assert(std::numeric_limi

c++ - 通过 union 在 C 和 C++ 中输入双关结构

我已经在gcc和g++中用pedantic编译了这个,我在任何一个中都没有收到警告:#include#include#includestructa{structa*next;inti;};structb{structb*next;inti;};structc{intx,x2,x3;union{structaa;structbb;}u;};voidfoo(structb*bar){bar->next->i=9;return;}intmain(intargc,char*argv[]){structcc;memset(&c,0,sizeofc);c.u.a.next=(structa*)ca

c++ - 实践中的 union 、别名和类型双关语 : what works and what does not?

我无法理解使用带有GCC的union可以做什么和不可以做什么。我阅读了有关它的问题(特别是here和here),但它们关注的是C++标准,我觉得C++标准和实践(常用的编译器)之间存在不匹配。特别是,我最近在GCConlinedoc中发现了令人困惑的信息。在阅读编译标志-fstrict-aliasing时。它说:-fstrict-aliasingAllowthecompilertoassumethestrictestaliasingrulesapplicabletothelanguagebeingcompiled.ForC(andC++),thisactivatesoptimizati

c++ - 实践中的 union 、别名和类型双关语 : what works and what does not?

我无法理解使用带有GCC的union可以做什么和不可以做什么。我阅读了有关它的问题(特别是here和here),但它们关注的是C++标准,我觉得C++标准和实践(常用的编译器)之间存在不匹配。特别是,我最近在GCConlinedoc中发现了令人困惑的信息。在阅读编译标志-fstrict-aliasing时。它说:-fstrict-aliasingAllowthecompilertoassumethestrictestaliasingrulesapplicabletothelanguagebeingcompiled.ForC(andC++),thisactivatesoptimizati

c++ - 类型双关、char[] 和取消引用

我有一个旨在存储用户定义数据(即来自插件)的结构。它有一个这样的char[],它具有给定的最大大小来存储该数据。structA{//othermembersomitted//datameanttobetypepunned,onlycontainsPODschardata[256];};然后是一个示例用户结构,它有一个静态函数,可以从A转换自身。structB{inti;doubled;staticB&FromA_ref(A&a){//static_assertthatsizeof(B)(a.data);}};我用g++-O3-std=c++0x-Wall-otesttest.cpp(G

c++ - 如何通过不同类型重新解释数据? (类型双关困惑)

#includeintmain(intargc,char*argv[]){inta=0x3f800000;std::cout(&a);std::cout(p);floatf3=*pf;std::cout(static_cast(&a));std::cout我从我可靠的编译器中得到以下信息:me@Mint-VM~/projects$g++-5.3.0-std=c++11-opunpun.cpp-fstrict-aliasing-Wallpun.cpp:Infunction‘intmain(int,char**)’:pun.cpp:11:45:warning:dereferencingty

c++ - "cast to first member of standard layout"类型双关规则是否扩展到数组?

具体来说,我将CAPI包装在一个友好的C++包装器中。CAPI具有这种相当标准的形式:structfoo{...};voidget_foos(size_t*count,foo*dst);我想做的是,通过将类型双关的包装器数组直接传递给Capi来为自己保存一个额外的拷贝,并保持理智检查static_assert().classfooWrapper{fooraw_;public:[...]};std::vectorget_foo_vector(){size_tcount=0;get_foos(&count,nullptr);std::vectorresult(count);//Isthis

c++ - 是否可以通过消除 >= 比较的需要,将有符号的双关符号输入无符号整数来加快边界检查?

假设我的程序中有一个对性能非常关键的循环,我需要检查一个点是否在一个矩形内,但我知道在编译时下限总是为0,如下所示:(x>=0&&y>=0&&x我是否可以通过将x和y键入无符号整数来消除前两个比较(例如,在C++中使用reinterpret_cast()或union之类的东西),因为符号位会保证任何负数都会变成足够大的unsignedint边界检查失败?如果是这样,您将如何用C++或其他语言来实现它?这样做可以提高性能吗? 最佳答案 是的,当您测试有符号整数并且下限为零时,这是一个完全有效的优化。事实上,这是一种常见的优化,你的编译
12