草庐IT

store_const

全部标签

c++ - g++ 将返回的字符串文字视为 const char 指针而不是 const char 数组

当从一个应该使用g++(版本4.7.3)执行隐式转换的函数返回字符串文字时,我看到了一些奇怪的行为。谁能解释为什么下面的代码:#includeclassTest{public:templateTest(constchar(&foo)[N]){printf("Templateconstchararrayconstructor\n");}Test(char*foo){printf("char*constructor\n");}};Testfn(){return"foo";}intmain(){Testt("bar");Testu=fn();return0;}产生结果:Templatecon

c++ - const_cast 未定义行为的重要示例

据我了解,以下代码是根据c++标准(特别是第7.1.5.1.4[dcl.type.cv]/4节)未定义的行为。#includestructF;F*g;structF{F():val(5){g=this;}intval;};constFf;intmain(){g->val=8;std::cout但是,我尝试过的每个编译器和优化设置都会打印“8”。问题:这种类型的“隐式const_cast”是否有会出现意外结果的示例?我希望有一些像结果一样壮观的东西#includeintmain(){for(inti=0;i开启,例如,带有-O2的gcc4.8.5EDIT:标准中的相关部分7.1.5.1.

c++ - const_cast 未定义行为的重要示例

据我了解,以下代码是根据c++标准(特别是第7.1.5.1.4[dcl.type.cv]/4节)未定义的行为。#includestructF;F*g;structF{F():val(5){g=this;}intval;};constFf;intmain(){g->val=8;std::cout但是,我尝试过的每个编译器和优化设置都会打印“8”。问题:这种类型的“隐式const_cast”是否有会出现意外结果的示例?我希望有一些像结果一样壮观的东西#includeintmain(){for(inti=0;i开启,例如,带有-O2的gcc4.8.5EDIT:标准中的相关部分7.1.5.1.

C++ 标准 : can relaxed atomic stores be lifted above a mutex lock?

标准中是否有任何措辞保证对原子的宽松存储不会被提升到互斥锁的锁定之上?如果没有,是否有任何措辞明确表示编译器或CPU这样做是符合犹太教规的?例如,采用以下程序(它可能使用acq/rel来处理foo_has_been_set并避免锁定,和/或使foo本身原子化。它是这样写的来说明这个问题。)std::mutexmu;intfoo=0;//Guardedbymustd::atomicfoo_has_been_set{false};voidSetFoo(){mu.lock();foo=1;foo_has_been_set.store(true,std::memory_order_relaxe

C++ 标准 : can relaxed atomic stores be lifted above a mutex lock?

标准中是否有任何措辞保证对原子的宽松存储不会被提升到互斥锁的锁定之上?如果没有,是否有任何措辞明确表示编译器或CPU这样做是符合犹太教规的?例如,采用以下程序(它可能使用acq/rel来处理foo_has_been_set并避免锁定,和/或使foo本身原子化。它是这样写的来说明这个问题。)std::mutexmu;intfoo=0;//Guardedbymustd::atomicfoo_has_been_set{false};voidSetFoo(){mu.lock();foo=1;foo_has_been_set.store(true,std::memory_order_relaxe

c++ - 为什么 "auto const&"不是只读的?

这个问题在这里已经有了答案:Variablesmarkedasconstusingstructuredbindingsarenotconst(1个回答)关闭4年前.#includeintmain(){intxa=1;intya=2;autoconst&[xb,yb]=std::tuple(xa,ya);xb=9;//Shouldn'tthisberead-only?returnxa+ya;}这不仅编译,而且返回11。那么两个问题:为什么当xb被指定为autoconst&时我可以写入?这不应该编译失败吗?为什么我不能用“auto&”替换“autoconst&”并让它编译?Clang(6.

c++ - 为什么 "auto const&"不是只读的?

这个问题在这里已经有了答案:Variablesmarkedasconstusingstructuredbindingsarenotconst(1个回答)关闭4年前.#includeintmain(){intxa=1;intya=2;autoconst&[xb,yb]=std::tuple(xa,ya);xb=9;//Shouldn'tthisberead-only?returnxa+ya;}这不仅编译,而且返回11。那么两个问题:为什么当xb被指定为autoconst&时我可以写入?这不应该编译失败吗?为什么我不能用“auto&”替换“autoconst&”并让它编译?Clang(6.

c++ - `unique_ptr< T const [] >` 是否应该接受 `T*` 构造函数参数?

代码:#includeusingnamespacestd;structT{};T*foo(){returnnewT;}Tconst*bar(){returnfoo();}intmain(){unique_ptrp1(bar());//OKunique_ptra1(bar());//OKunique_ptrp2(foo());//OKunique_ptra2(foo());//?thisisline#15}VisualC++10.0和MinGWg++4.4.1的示例错误:[d:\dev\test]>clfoo.cppfoo.cppfoo.cpp(15):errorC2248:'std::

c++ - `unique_ptr< T const [] >` 是否应该接受 `T*` 构造函数参数?

代码:#includeusingnamespacestd;structT{};T*foo(){returnnewT;}Tconst*bar(){returnfoo();}intmain(){unique_ptrp1(bar());//OKunique_ptra1(bar());//OKunique_ptrp2(foo());//OKunique_ptra2(foo());//?thisisline#15}VisualC++10.0和MinGWg++4.4.1的示例错误:[d:\dev\test]>clfoo.cppfoo.cppfoo.cpp(15):errorC2248:'std::

c++ - const对象的构造

C++11§12.1/14:Duringtheconstructionofaconstobject,ifthevalueoftheobjectoranyofitssubobjectsisaccessedthroughanlvaluethatisnotobtained,directlyorindirectly,fromtheconstructor’sthispointer,thevalueoftheobjectorsubobjectthusobtainedisunspecified.[Example:structC;voidno_opt(C*);structC{intc;C():c(0)