草庐IT

static_pointer_cast

全部标签

c++ - 使用 reinterpret_cast 将函数强制转换为 void*,为什么不违法?

这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher

c++ - 使用 reinterpret_cast 将函数强制转换为 void*,为什么不违法?

这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher

C++ 多线程 : is initialization of a local static lambda thread safe?

这个问题在这里已经有了答案:GCC'sTSANreportsadataracewithathreadsafestaticlocal(1个回答)关闭5年前。C++11标准说明局部静态变量初始化应该是线程安全的(http://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables)。我的问题是当lambda被初始化为静态局部变量时究竟会发生什么?让我们考虑以下代码:#include#includeintdoSomeWork(intinput){staticautocomputeSum=[](int

C++ 多线程 : is initialization of a local static lambda thread safe?

这个问题在这里已经有了答案:GCC'sTSANreportsadataracewithathreadsafestaticlocal(1个回答)关闭5年前。C++11标准说明局部静态变量初始化应该是线程安全的(http://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables)。我的问题是当lambda被初始化为静态局部变量时究竟会发生什么?让我们考虑以下代码:#include#includeintdoSomeWork(intinput){staticautocomputeSum=[](int

c++ - reinterpret_cast moSTLy 没用吗?

我已经阅读了关于theuseofreinterpret_cast的各种先前问题。,并且我还阅读了C++标准中的相关措辞。本质上,它归结为指针到指针reinterpret_cast操作的结果不能安全地用于任何东西,除了被转换回原始指针类型。然而,在实践中,reinterpret_cast的大多数实际使用似乎是基于(错误的)假设,即reinterpret_cast与C风格相同throw。例如,我看到很多代码使用reinterpret_cast将char*转换为unsignedchar*以进行字符集转换例行公事。这是完全无害的,但严格来说它不是可移植的-不能保证从char*到unsigned

c++ - reinterpret_cast moSTLy 没用吗?

我已经阅读了关于theuseofreinterpret_cast的各种先前问题。,并且我还阅读了C++标准中的相关措辞。本质上,它归结为指针到指针reinterpret_cast操作的结果不能安全地用于任何东西,除了被转换回原始指针类型。然而,在实践中,reinterpret_cast的大多数实际使用似乎是基于(错误的)假设,即reinterpret_cast与C风格相同throw。例如,我看到很多代码使用reinterpret_cast将char*转换为unsignedchar*以进行字符集转换例行公事。这是完全无害的,但严格来说它不是可移植的-不能保证从char*到unsigned

c++ - 错误 : called object type 'int' is not a function or function pointer

我在这里写了一个快速排序:voidswap(int&a,int&b);intmid(intlo,inthi);//Myquicksortimplementationvoidsort(intvec[],intlo,inthi){intmid;if(hi>lo){inti=lo+1;intj=hi;intp=mid(lo,hi);swap(vec[lo],vec[p]);mid=vec[lo];while(i=mid);swap(vec[i],vec[j]);}}i++;swap(vec[lo],vec[i]);sort(vec,lo,i);sort(vec,j,hi);}}voidswa

c++ - 错误 : called object type 'int' is not a function or function pointer

我在这里写了一个快速排序:voidswap(int&a,int&b);intmid(intlo,inthi);//Myquicksortimplementationvoidsort(intvec[],intlo,inthi){intmid;if(hi>lo){inti=lo+1;intj=hi;intp=mid(lo,hi);swap(vec[lo],vec[p]);mid=vec[lo];while(i=mid);swap(vec[i],vec[j]);}}i++;swap(vec[lo],vec[i]);sort(vec,lo,i);sort(vec,j,hi);}}voidswa

c++ - SFINAE:static_assert 与 std::enable_if

以下(建议!)语法有什么缺点吗?templatevoidf()static_assert(std::is_same::value){;}而不是SFINAE(看起来像拐杖):template::value>::type>voidf(){;}甚至更糟:templatetypenamestd::enable_if::value>::typef(){;}其中禁止使用auto推导结果类型。 最佳答案 首先,它们是不同的,特别是它们不是同时检查的。关键的区别在于它们在重载解析方面的应用。SFINAE将从重载集中剔除函数,以便选择另一个函数(如果

c++ - SFINAE:static_assert 与 std::enable_if

以下(建议!)语法有什么缺点吗?templatevoidf()static_assert(std::is_same::value){;}而不是SFINAE(看起来像拐杖):template::value>::type>voidf(){;}甚至更糟:templatetypenamestd::enable_if::value>::typef(){;}其中禁止使用auto推导结果类型。 最佳答案 首先,它们是不同的,特别是它们不是同时检查的。关键的区别在于它们在重载解析方面的应用。SFINAE将从重载集中剔除函数,以便选择另一个函数(如果