草庐IT

objc_setProperty_atomic_copy

全部标签

c++ - 不同类型的 copy_if

如果我知道如何提取匹配类型,是否有一种现代方式来表达有条件地从不同类型的源容器复制到目标容器的意图?将问题作为代码示例提出更容易:#include#includestructFoo{};structFooBar{boolis_valid;Foofoo;};std::vectorget_valid_foos(conststd::vector&foobars){std::vectorvalid_foos;for(constauto&fbar:foobars){if(fbar.is_valid)valid_foos.push_back(fbar.foo);}returnvalid_foos;

c++ - copy-and-swap 习语的低效率?

我正在测试一些代码,其中类中有一个std::vector数据成员。该类既可复制又可移动,operator=的实现方式如here所述。使用copy-and-swap习语。如果有两个vector,比如v1大容量,v2小容量,v2被复制到v1(v1=v2),赋值后保留v1中的大容量;这是有道理的,因为接下来的v1.push_back()调用不必强制进行新的重新分配(换句话说:释放已经可用的内存,然后重新分配它以增加vector没有多大意义).但是,如果对以vector为数据成员的类进行相同的赋值,则行为不同,并且在赋值之后更大的容量是不保留。如果copy-and-swap惯用语不被使用,复制

c++ - 为什么 `std::copy` 在我的测试程序中从 char 缓冲区读取一个 int 比 `memcpy` 慢 5 倍(!)?

这是thisquestion的后续行动我在哪里发布了这个程序:#include#include#include#include#include#include#include#include#includeclassStopwatch{public:typedefstd::chrono::high_resolution_clockClock;//!ConstructorstartsthestopwatchStopwatch():mStart(Clock::now()){}//!Returnselapsednumberofsecondsindecimalform.doubleelapse

c++ - std::atomic<uint_least8_t> 行为

在一个系统上:typedefunsignedcharuint8_t;typedefunsignedshortuint16_t;std::atomic::is_always_lock_free//=>falsestd::atomic::is_always_lock_free//=>true据我了解,类型std::atomic将是8位并且不是无锁的。如果是这样,如果我想要一个至少8位且始终无锁的原子类型,我应该怎么写?(假设存在这种类型)是否有比以下更好的选择:std::atomic::is_always_lock_free,uint8_t,uint16_t>::type>(为简单起见,我

c++ - 重用 copy-and-swap 习惯用法

我正在尝试将copy-and-swap习惯用法放入可重用的混音中:templatestructcopy_and_swap{Derived&operator=(Derivedcopy){Derived*derived=static_cast(this);derived->swap(copy);return*derived;}};我打算通过CRTP将其混入:structFoo:copy_and_swap{Foo(){std::cout然而,一个简单的测试表明它不起作用:Foox;Fooy;x=y;这只会打印两次“default”,既不会打印“copy”也不会打印“swap”。我在这里缺少什

c++ - 为什么 std::copy_if 签名不约束谓词类型

假设我们有以下情况:structA{inti;};structB{Aa;intother_things;};boolpredicate(constA&a){returna.i>123;}boolpredicate(constB&b){returnpredicate(b.a);}intmain(){std::vectora_source;std::vectorb_source;std::vectora_target;std::vectorb_target;std::copy_if(a_source.begin(),a_source.end(),std::back_inserter(a_t

c++ - 如何正确使用 std::atomic_signal_fence()?

cppreference.com将此函数记录为“线程与在同一线程中执行的信号处理程序之间的栅栏”。但是网上没找到例子。我想知道以下伪代码是否正确说明了std::atomic_signal_fence()的功能:intn=0;SignalObjects;voidthread_1(){s.wait();std::atomic_signal_fence(std::memory_order_acquire);assert(1==n);//neverfires???}voidthread_2(){n=1;s.signal();}intmain(){std::threadt1(thread_1);

c++ std::copy 类型转换为派生类可能吗?

我很确定没有办法明确地做到这一点,但我还是想问一下,以防万一有更好的方法。我有一个基类A和一个派生类B,现在我有一个指向B*的A*std::list,我想将这个A*列表复制到B的std::vector*基本上我想这样做:std::listaList=someObject.getAs();std::vectorbVec=std::vector(aList.begin(),aList.end());我很确定当列表和vector是相同类型时(例如,都是A*的)这应该可以编译,但是因为在这种情况下A*是B*的基类,所以我不能这样做这样,因为我必须像这样显式地进行类型转换:std::listaL

c++ - 为什么 std::mutex 比 std::atomic 快?

我想在多线程模式下将对象放入std::vector中。所以我决定比较两种方法:一种使用std::atomic,另一种使用std::mutex。我看到第二种方法比第一种方法更快。为什么?我使用GCC4.8.1,在我的机器(8线程)上,我看到第一个解决方案需要391502微秒,第二个解决方案需要175689微秒。#include#include#include#include#include#includeintmain(intargc,char*argv[]){constsize_tsize=1000000;std::vectorfirst_result(size);std::vecto

c++ - 是否可以哄 std::atomic<T> 输出 CMPXCHG16B

对于我对在Windowsx64上使用原子互锁操作不感兴趣的类型,是否可以哄骗std::atomic输出CMPXCHG16B,或者我是否只需要接受它并手动执行原子操作?我可以让GCC/Clang在Linux上执行此操作,所以我怀疑这只是Microsoft标准库的问题。structByte16{int64_ta,b;};std::atomicatm;Byte16a={1,2};atm.compare_exchange_strong(...);//ThishasalockonWindows,notonLinuxversionofcode 最佳答案