草庐IT

c++ - std::bind() - 从派生类的成员函数中获取基保护成员函数

我想从派生类bind()到我的基类版本的函数。该功能在基础中被标记为protected。当我这样做时,代码在Clang(AppleLLVM编译器4.1)中编译愉快,但在g++4.7.2和VisualStudio2010中都出现错误。错误如下:“'Base::foo':不能访问protected成员。”这意味着引用的上下文实际上在bind()中,当然函数在其中被视为protected。但是bind()不应该继承调用函数的上下文——在本例中是Derived::foo()——因此将基方法视为可访问的?下面的程序说明了这个问题。structBase{protected:virtualvoidf

c++ - std::move 在 boost 库中的对应物

我正在尝试在我的代码中使用std::move,但我使用的编译器(g++4.4)不支持它。boost::move可以完全替代std::move吗?谢谢。 最佳答案 std::move(和boost::move当启用c++0x支持时)只是来自T&的转换至T&&.它实际上并没有移动任何东西。这意味着指针的具体类型T&&必须得到编译器的支持。GCC从4.3版本开始支持右值引用,所以boost版本应该没问题。但是,有没有理由不能使用std::move来自#include?http://en.cppreference.com/w/cpp/uti

c++ - 为什么范围的算法与 std 的迭代器不兼容?

#include#include#includeintmain(){autocoll=std::vector{1,2,3};ranges::copy(coll,ranges::ostream_iterator{std::cout,","});//okranges::copy(coll,std::ostream_iterator{std::cout,","});//error}上面的代码显示了这个问题。我用ranges-v3-0.3.7.对我来说,通用算法copy不应该关心目标迭代器类型,只要它满足输出迭代器的要求即可。如果是这样,为什么范围的算法与std的迭代器不兼容?

c++ - 将空终止的 const char* 字符串数组转换为 std::vector< std::string >

我有一个VisualStudio2008C++函数,我在其中获得了一个空终止字符串数组constchar*以及该数组中字符串的数量。我正在寻找一种巧妙的方法来转换constchar*的数组在std::vector///@paramcount-numberofstringsinthearray///@paramarray-arrayofnull-terminatedstrings///@return-avectorofstlstringsstd::vectorConvert(intcount,constchar*array[]);Boost可以,STL可以。

c++ - std::async 可以与模板函数一起使用吗

是std::async假设使用模板函数?我试过启动std::reverse作为一个异步任务,bu得到了编译时错误。我尝试使用更简单的函数(foo和bar)并发现只有非模板函数在工作。#include#include#includevoidfoo(std::string::iteratorfirst,std::string::iteratorlast){}templatevoidbar(BidirectionalIteratorfirst,BidirectionalIteratorlast){}intmain(){std::stringstr="Loremipsum,dolorsitam

c++ - g++ 4.7.2 中缺少 std::stoi?

当我尝试使用std::stoi并尝试编译它时,我收到错误消息“stoi不是std的成员”。我从命令行使用g++4.7.2,所以它不会是IDE错误,我的所有包含都按顺序排列,g++4.7.2默认使用c++11。如果有帮助,我的操作系统是Ubuntu12.10。有什么我没有配置的吗?#include#includeusingnamespacestd;intmain(){stringtheAnswer="42";intans=std::stoi(theAnswer,0,10);cout不会编译。但它没有任何问题。 最佳答案 std::st

c++ - std::default_random_engine 生成介于 0.0 和 1.0 之间的值

我希望能够生成介于0.0和1.0之间的随机值我试过std::default_random_enginegenerator;std::uniform_real_distributiondistribution(0.0,1.0);floatmyrand=distribution(generator);在循环中生成随机值总是给我这些值:0.0000220.0850320.6013530.8916110.9679560.1896900.5149760.3980080.2629060.7435120.089548我该怎么做才能真正获得随机值?如果我总是得到相同的,那似乎不是随机的。

C++11 'native_handle' 不是 'std::this_thread' 的成员

在下面的代码片段中,voidfoo(){std::this_thread::native_handle()....//errorhere}intmain(){std::threadt1(foo);t1.join();return0;}如何从函数foo中的std::this_thread获取native_handle? 最佳答案 线程无法自动获得对其自身std::thread的访问权。这是有意为之的,因为std::thread是一种只能移动的类型。我相信您要求的是std::thread::id的native_handle()成员,这是

c++ - 什么时候使用 std::function 而不是继承?

在某些情况下,std::function可以替代继承。以下两个代码片段非常相似(调用函数时的成本大致相同,签名中的用法几乎相同,并且在大多数情况下std::function不需要我们制作A的额外拷贝以及):structFunction{virtualintoperator()(int)const=0;};structA:publicFunction{intoperator()(intx)constoverride{returnx;}};使用std::function,我们可以将其重写为usingFunction=std::function;structA{intoperator()(i

c++ - C++ 标准容器的线程安全

我在这里阅读了很多关于C++的标准容器(如“list”或“map”)是否线程安全的帖子,他们都说这不是一般的。并行读取应该没问题,但是并行写入或并行读取和写入可能会导致问题。现在我发现在www.cplusplus.com在大多数操作期间访问或修改列表是安全的。一些例子:map::find容器被访问(const和非const版本都不会修改容器)。没有访问映射值:并发访问或修改元素是安全的。map::insert容器被修改。并发访问现有元素是安全的,尽管在容器中迭代范围是不安全的。我是否误解了cplusplus.com,或者关于std容器中的线程安全还有什么我必须知道的。提前致谢!PS:我