草庐IT

copy-initialization

全部标签

c++ - 为什么我不能直接将 to_lower_copy 传递给 transform 而不是将其包装在 lambda 中?

我正在尝试使用boost::to_lower_copy和std::transform来小写一堆字符串。如下,变体1,使用lamdba工作;变体2还可以证明这是编译器选择的正确模板重载。但是lambda很傻——它所做的只是将单个参数转发给boost::to_lower_copy。但是变体3,直接使用函数模板不会编译,即使我实例化它。我错过了什么?我有clang版本3.3(tags/RELEASE_33/rc3),使用libstdc++-4.8.1-1.fc19.i686和boost-1.53​​.0-14.fc19.i686。vectorstrings={"Foo","Bar"};vec

解决es报错:the shard cannot be allocated to the same node on which a copy of the shard already exists

一:报错信息通过命令:curl-XGET"http://{ip}:9200/_cluster/allocation/explain"查看集群状态:可以看到其active_shards_percent为36.1%,elasticsearch健康状态为yellow,原因就是其存在UNASSIGNEDshards的情况,而此时也影响到了es的正常使用。二、分析原因:如果我们只有一台机器,部署运行了es,但是却在index的settings中设置了replica为1,那么这个replicashard就会成为unassignedshards,因为分片不能分配到已经存在分片副本的同一节点.而当我们在查看原

c++ - G++ 4.5 错误 : No diagnostic for narrowing in initializer list

我尝试了以下代码:intmain(){intx{23.22};}其中包括需要缩小的初始化,但代码编译正常,没有任何错误或警告。另一方面,以下代码给出了错误:intmain(){intx[]{23.22};}我是发现了错误还是什么?PS:我目前使用的是GCC4.5.0 最佳答案 看起来像一个错误。以下直接来自n3092草案:8.5.4List-initialization—Otherwise,iftheinitializerlisthasasingleelement,theobjectisinitializedfromthatelem

c++ - 模板函数 : default construction without copy-constructing in C++

考虑structC{C(){printf("C::C()\n");}C(int){printf("C::C(int)\n");}C(constC&){printf("copy-constructed\n");}};还有一个模板函数templatevoidfoo(){//default-constructatemporaryvariableoftypeT//thisiswhatthequestionisabout.Tt1;//willbeuninitializedfore.g.int,float,...Tt2=T();//willcalldefaultconstructor,thenco

c++ - 如何在成功或失败的情况下返回 std::copy 的值?

我正在使用std::copy将std::deque中的对象复制到一个文件中。代码运行良好,但我需要检查复制是否成功,因此我需要设置标志或抛出异常。我用谷歌搜索但找不到如何检查std::copy是否已成功将值复制到文件中的解决方案。有人可以给它点亮吗。 最佳答案 如果写入文件失败,则文件流的错误标志将被设置-您可以在复制后检查这些,或者先调用exceptions成员函数使其抛出异常错误。如果其他方法失败,则会抛出异常。 关于c++-如何在成功或失败的情况下返回std::copy的值?,我们

c++ - 如果用户使用 Rice 在 Ruby 中重新定义 initialize(),则避免 C++ 代码中的段错误

在为Ruby编写C++扩展时,我一直在努力解决的一个问题是,即使用户做了愚蠢的事情,也要让它真正安全。那时他应该得到异常,但绝不是SegFault。一个具体的问题如下:我的C++类有一个非平凡的构造函数。然后我使用RiceAPI来包装我的C++类。如果用户在他的Ruby代码中重新定义了initialize(),那么Rice创建的initialize()函数就会被覆盖,对象既不会被分配也不会被初始化。一个玩具示例如下:classPerson{public:Person(conststring&name):m_name(name){}conststring&name()const{retu

c++ - constexpr if with initializer 由标准保证吗? 'constexpr(constexpr auto x = f(); x) { }'

我找不到任何关于新C++17if初始化语法的信息和“constexprif”在:http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html不过,Clang-HEAD支持该语法...constexprautof(){returntrue;}intmain(){ifconstexpr(constexprautox=f();x){}}在线代码在这里->http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr是constexprif带有标准保证的初始值设定项,如constexpr

c++ - 错误 : has initializer but incomplete type

我遇到了问题:在使用结构时有初始化器但类型不完整:在hpp文件中:classA{private:structvideoDT;};在一个cpp文件中:structA::videoDT{videoDT(intb):a(b){}inta;};voidtest(){structvideoDTtest(1);}那么问题来了:错误:有初始化器但类型不完整提前致谢 最佳答案 我认为问题在于test()无法访问A的私有(private)类型。这为我编译:classA{private:friendvoidtest();structvideoDT;};

c++ - 通过 copy-and-swap 分配与两个锁

借款HowardHinnant'sexample并将其修改为使用copy-and-swap,这op=线程安全吗?structA{A()=default;A(Aconst&x);//Assumeimplementscorrectlockingandcopying.A&operator=(Ax){std::lock_guardlock_data(_mut);usingstd::swap;swap(_data,x._data);return*this;}private:mutablestd::mutex_mut;std::vector_data;};我相信这是线程安全的(记住op=的参数是按

c++ - Visual Studio C++ - 缺少 "Copy to Output Directory"

这个问题在这里已经有了答案:Automaticcopyfilestooutputduringapplicationbuilding(8个答案)关闭9年前。我在VisualStudio(2012)中创建了一个空的C++项目,当我在解决方案资源管理器中选择了某个文件时,在“属性”窗口中看不到“复制到输出目录”选项。为什么?