草庐IT

c++ - 我们是否需要保护单个赋值或 if 线程安全语句

假设我有:staticintwrite_log=0;void*logger__run(void*arg){//loggerthreadexecution.while(1){//getlogmessagefromsharedqueue.if(write_log){//justcheckingwrite_logvalue.//writelogstillwrite_logistrue.}//destroylogmessage.}}voidlogger__set_logging(intp_write_log){//otherthreadscanstart/stoploggingbylogger

C++11/14 : Wrap a function if it exists

我想编写一个wrapper类(非常像一个代理)来聚合一个对象,并将成员函数调用转发给它。在使用可变参数模板和decltype的C++11/14中,这很简单。我的问题是包装对象可能支持也可能不支持某些成员函数。我想出了一个似乎有效的解决方案,但是,它看起来非常笨拙,我正在寻找简化方法。特别是我担心这在编译时可能会非常昂贵(有许多函数要包装)。这种笨拙是因为需要指定函数的返回类型,而无需decltype某些令人窒息的内容。有人有更好的主意吗?下面这段代码也可用live.#include#include///Computetheresulttypeofamemberfunctioncall,

c++ - 不可复制和 move 构造函数

我已经使一个类的成员不可复制,但我给它一个move构造函数和赋值运算符。然而,它不会像vector一样与容器打球。classNonCopyable{public:NonCopyable(constNonCopyable&)=delete;NonCopyable&operator=(constNonCopyable&)=delete;protected:NonCopyable(){}~NonCopyable()_NOEXCEPT{}};classMember:NonCopyable{public:Member(inti):mNum(i){}~Member(){}Member(Member

c++ - 惰性构造 - 虚拟方法与 if-then stub setter/getter

我的问题的背景是我试图创建一个惰性网格结构,其中网格区域仅在需要时实例化,否则它们在查询时返回默认值。稍微归结一下这个问题,考虑一下我的情况的以下模型:structContainer{std::vectordata;floatget(intindexOuter,intindexInner){returndata[indexOuter].get(indexInner);}}我想stubBase::get在某些情况下函数总是返回相同的值,而在其他情况下我想返回某个数组中的值。我想象两种可能的解决方案。第一个解决方案是在Base上使用标志,即structBase{std::vectordat

c++ - 我怎么知道我使用的是复制还是 move ?

我对C++14标准库使用move语义的理解正确吗?换句话说,我可以确信我在以下程序中使用的是move而不是复制:#include#include#includeusingnamespacestd::string_literals;std::vectorgreeting(){std::vectorvs{"hello"s,"world"s};returnvs;}intmain(){std::vectors=greeting();std::cout有什么方法可以检查吗?在下面的例子中怎么样:#include#include#includeusingnamespacestd::string_l

c++ - [conv]/6中语句 "The expression e is used as a glvalue if and only if the initialization uses it as a glvalue"的确切含义是什么

[conv]/6(重点是我的):Theeffectofanyimplicitconversionisthesameasperformingthecorrespondingdeclarationandinitializationandthenusingthetemporaryvariableastheresultoftheconversion.TheresultisanlvalueifTisanlvaluereferencetypeoranrvaluereferencetofunctiontype([dcl.ref]),anxvalueifTisanrvaluereferencetoob

c++ - 当从函数返回元组时,元组的参数被复制而不是 move

我对以下代码有疑问。我的编译器是MSVC++17VisualStudio15.3版,编译器选项为/std:c++14(相对于/std:c++latest),在Release模式下运行:structBar{inta;std::stringb;Bar(){std::coutfoo(){std::strings="dsdf";return{{1,s},{5,"asdf"}};}intmain(){Bara,b;std::tie(a,b)=foo();std::cout输出是:defaultdefaultdirectdirectmovebconstcopyconstcopymoveassign

带有 std::enable_if 的 C++ 可变参数模板部分模板特化

ITNOA我的问题是如何在可变参数模板部分模板特化场景中使用std::enable_if?例如,我有一个类使用如下所示的可变参数模板部分特化/***Commoncase.*/templatestructfoo;/***Finalsuperclassforfoo.*/templatestructfoo{voidfunc(){}};/***Regularfooclass.*/templatestructfoo:publicfoo{typedefsuperfoo;voidfunc(){coutsuper::templatefunc();}}它工作正常,但如果H是整数类型,我想要特定的部分特化

c++ - gsl_vector 有 count_if 函数吗? C/C++

我正在使用gnu科学图书馆(GSL)。假设我有一个像这样的gsl_vector:70-658010-2这是一个包含正数、负数和零作为元素的vector。我想统计这个gsl_vector中非零元素或零元素的个数。我知道C++Vector有一个名为count_if的函数。但是我搜索了gsl_vector.h和gsl_blas.h,没有能与之匹敌的功能。我可以通过gsl_vector_get()评估它们来遍历所有元素,然后问if问题。intcounter=0;for(inti=0;i但是我想了将近一天,GSL中是否已经有这样一个效率更高的函数。或者gsl_array有一个count_if函数

C++11 - 单独 move 数组(原始数组、std::array、std::vector)的每个元素?

在C++11中,move语义等等,人们可能想知道实际上可以move什么。这方面的一个例子是数组。是否可以move原始数组的每个元素,intarray1[8];intarray2[8];array1[0]=std::move(array2[0]);std::数组,std::arrayarray1;std::arrayarray2;array1[0]=std::move(array2[0]);和std::vectorsstd::vectorarray1;std::vectorarray2;array1[0]=std::move(array2[0]);个人? 最佳