草庐IT

c++11 to_string 与 code::blocks -std=c++11 flag already selected

这是我正在尝试编译的代码,是从某个地方的另一个论坛获得的。//to_stringexample#include//std::cout#include//std::string,std::to_stringintmain(){std::stringpi="piis"+std::to_string(3.1415926);std::stringperfect=std::to_string(1+2+4+7+14)+"isaperfectnumber";std::cout我收到错误:'to_string'不是'std'的成员我在其他论坛上读到选择标志“让g++遵循c++11ISO语言标准[-st

c++ - 这种在 C++03 中模拟 move 语义的方法有多安全?

使用thisanswer,我发明了自己的基于swap的C++03模拟move语义的方法。首先,我检测move语义(即C++03的可用性):#if__cplusplus>=201103L||defined(__GXX_EXPERIMENTAL_CXX0X__)||\defined(_MSC_VER)&&_MSC_VER>=1600#defineHAS_MOVE_SEMANTICS1#elifdefined(__clang)#if__has_feature(cxx_rvalue_references)#defineHAS_MOVE_SEMANTICS1#else#defineHAS_MOV

c++ - 如何从 std::basic_ios 为 OS X 上的 clang 获取文件描述符?

我正在将一些代码移植到DarwinOSX,作为更改的一部分,我们从gcc转到clang编译器。代码中有一个2005年的函数,在网上多处贴过。它为几个不同的旧版本GCC提供了功能,我已经编辑了除最后一个版本之外的所有功能,v3.4.0或更高版本。该代码取决于两个GCC特定类:__gnu_cxx::stdio_filebuf和__gnu_cxx::stdio_sync_filebuf。//!Similartofileno(3),buttakingaC++streamasargumentinsteadofa//!FILE*.Notethatthereisnowayforthelibraryt

c++ - 为什么 std::bind 不考虑功能元数?

如果我有这个简单的案例:structFoo{voidbar();voidbaz(int);};这会编译是有道理的:Foofoo;autof=std::bind(&Foo::bar,&foo);但是为什么bind会被设计成这样:autog=std::bind(&Foo::baz,&foo);我可以调用f,但我永远不能调用g。为什么还要进行编译?要求我必须这样做的理由是什么:autog2=std::bind(&Foo::baz,&foo,std::placeholders::_1);如果你想弄乱哪些参数被传递以及以什么顺序传递,我可以理解使用占位符,但为什么不让默认传递所有参数无需指定的正

c++ - 调用 get() 后 std::future 仍然有效(抛出异常)

根据cppreference,在调用std::future::get之后:valid()isfalseafteracalltothismethod.此外,来自cplusplus.com:Oncethesharedstateisready,thefunctionunblocksandreturns(orthrows)releasingitssharedstate.Thismakesthefutureobjectnolongervalid:thismemberfunctionshallbecalledonceatmostforeveryfuturesharedstate.在异常安全下:Th

c++ - 如何在 "two dimensional manner"中使用 boost::variant 定义异构 std::map

我很乐意得到并建议如何以“二维方式”处理boost::variant。听起来很奇怪,但让我的代码说得更多(希望如此):我编写了一个名为Parameter的类:templateclassParameter:publicquantity{...}上面定义的我的参数的示例用法:ParameterSampleParameter1;ParameterSampleParameter2;正如我试图通过上面的示例解释的那样,我可以使用boost::units::si::???和不同的数据类型(如double,short)定义多个参数类型,int等我的目标是构建一个std::map容器,它可以存储任何P

c++ - 为什么 std::generate 可以在没有命名空间限定符的情况下访问?

这样编译正常正常吗?#include#includeintmain(){std::vectorbuf;generate(buf.begin(),buf.end(),[]{return0;});}(注意generate()前面缺少的std::)是否在某处记录了此行为?还是我偶然发现了编译器或库错误?在我的例子中,Linux上的GCC5.3.0和Clang3.8.0;两者都使用libstdc++,所以可能是库错误? 最佳答案 这是允许的,主要是因为generate的参数在std中。代码如下namespaceFoo{structB{};v

c++ - std::make_shared 与 throw dtor 和 libc++ 不编译

这是非常基本的代码:#includeclassfoo{public:~foo()noexcept(false){}};intmain(){autox=std::make_shared();return0;}编译如下:g++-std=c++11test.cpp当使用libc++编译时,它会失败:/usr/bin/../include/c++/v1/memory:3793:7:error:exceptionspecificationofoverridingfunctionismorelaxthanbaseversionclass__shared_ptr_emplace^/usr/bin/.

c++ - `boost::any` 和 `std::any` 之间的差异

C++17引入了对象容器std::any,基于boost库boost::any.我的问题是:标准化的any是否等同于boost版本,或者是否存在差异?Asimilarquestionhasbeenpostedaboutvariant,在这种情况下存在一些差异,但我找不到关于any的引用。编辑:我能看到的一个区别是方法emplace的可用性。我对行为和保证之间的差异感兴趣的不仅仅是API的差异。例如,不同的分配对我来说很重要。 最佳答案 I'minterestedtothedifferencesbetweenthebehavioran

c++ - ostream_iterator vs for each 循环效率

我看到这个用户postyesterday.我认为这是输出vector的一种很酷的方式。所以我输入了一个示例并问自己这与foreach循环相比如何?templatevoidprintVectorO(std::vector&v){std::coutost(std::cout,"");std::copy(begin(v),end(v),ost);std::cout(time);std::coutvoidprintVectorC(std::vector&v){std::cout(time);std::cout我用了3个vector来测试这个:std::vectordoubles={3.15,2