草庐IT

-std=gnu99

全部标签

c++ - 使用 std::aligned_storage 对齐静态数组

我正在尝试使用std::aligned_storage模式实现简单静态数组的16字节对齐:#includeintmain(){constsize_tSIZE=8;usingfloat_16=std::aligned_storage::type;float_16mas;new(&mas)float[SIZE];//Placementnew.Isthisnecessary?mas[0]=1.f;//Compileerrorwhileattemptingtosetelementsofalignedarray}我得到以下编译错误:nomatchfor«operator[]»in«mas[0]»

c++ - std::tolower 和 Visual Studio 2013

我试着了解如何使用std::tolower...#include#include#include#includeintmain(){std::stringtest="HelloWorld";std::localeloc;for(auto&c:test){c=std::tolower(c,loc);}std::transform(test.begin(),test.end(),test.begin(),::tolower);//1)OKstd::transform(test.begin(),test.end(),test.begin(),std::tolower);//2)Cryptic

c++ - std::function 在移动后是否重置其内部功能

#includeusingnamespacestd;#includetemplateclassScopeExitFunction{public:ScopeExitFunction(F&func)throw():m_func(func){}ScopeExitFunction(F&&func)throw():m_func(std::move(func)){}ScopeExitFunction(ScopeExitFunction&&other)throw():m_func(std::move(other.m_func)){//other.m_func=[]{};}~ScopeExitFunc

c++ - std::chrono::duration_cast - 有比纳秒更精确的单位吗?

我想问的是,如何以皮秒、飞秒等任何单位计算时间,甚至更精确。我正在计算函数的运行时间并使用纳秒,当我使用毫秒或纳秒时,函数的运行时间返回0。我认为Chrono库只支持到纳秒,这是我在输入chrono::后按下ctrl+space时出现的最精确的时间:intmain(){autot1=std::chrono::high_resolution_clock::now();f();autot2=std::chrono::high_resolution_clock::now();std::cout(t2-t1).count()代码来源:http://en.cppreference.com/w/c

c++ - 在 C++ 中重载命名空间 std 中的数学函数是一种好习惯吗

我正在编写一个表示算术类型的C++类(围绕mpfr的C++包装器),我想支持中的一些函数(我将以std::sqrt为例).所以我有以下类(class):namespacens{classMyClass{/*...*/public:friendMyClasssqrt(constMyClass&mc);};}我可以这样使用它:MyClassc;/*...*/MyClassd=ns::sqrt(c);MyClasse=sqrt(c);//ApparentlyIdon'thavetospecifyns::但我不能这样使用它:MyClassf=std::sqrt(c);编译器(g++(Debia

c++ - 是什么导致 std::sort() 访问超出范围的地址

我明白要使用std::sort(),比较函数必须严格弱序,否则会因为访问地址越界而崩溃。(https://gcc.gnu.org/ml/gcc-bugs/2013-12/msg00333.html)但是,当比较函数不是严格的弱顺序时,为什么std::sort()会访问越界地址?它试图比较什么?我还想知道STL中是否还有其他我应该注意的陷阱。 最佳答案 首先是用不符合要求的比较器调用算法是未定义的行为,任何事情都会发生......但除此之外,我假设您有兴趣了解如果比较器不好,哪种类型的实现最终可能会越界访问。实现是否应该在访问元素之前

c++ - CMake 链接错误 pthread:启用多线程以使用 std::thread:不允许操作

我有一个类似的错误C++Threads,std::system_error-operationnotpermitted?我正在使用完全相同的源代码并使用进行编译g++../src/main.cpp-pthread-std=c++11工作没有任何问题。因为我想在一个更大的项目中使用线程,所以我必须在CMake中使用线程。搜索解决方案后,我发现了几个代码,例如:cmake_minimum_required(VERSION2.6)project(Test)add_definitions("-std=c++11")find_package(Threads)add_executable(main

c++ - 没有合适的用户定义的从 utility::string_t 到 std::string 的转换

我正在使用casablancaC++Rest库发出HTTP请求。问题是这给出了一个utility::string_t字符串作为输出,我无法找到任何方法将其转换为经典的std::string。有什么想法吗?client.request(methods::GET).then([](http_responseresponse){if(response.status_code()==status_codes::OK){string_ts=response.extract_string().get();}}); 最佳答案 根据您正在编译的平台

c++ - 检查模板参数是否为 std::vector<T>::iterator

如何检查模板参数是否为std::vector::iterator?对于void类型,我们有std::is_void。std::vector::iterator有类似的东西吗?? 最佳答案 你可以为此创建一个特征:#include#include#includetemplatestructis_vector_iterator:std::is_same::iterator>{};templatestructis_vector_iterator(),std::enable_if_t::iterator>::value>())>:std::

c++ - `std::lock_guard<std::mutex>` 对象没有名称时的不同行为

我正在了解std::mutex,std::thread我对下面两段代码的不同行为感到惊讶:#include#include#includeusingnamespacestd;std::mutexmtx;voidfoo(intk){std::lock_guardlg{mtx};for(inti=0;i输出是顺序的。但是如果我不命名变量std::lock_guard,输出是无序的voidfoo(intk){std::lock_guard{mtx};//justerasethenameofvariablefor(inti=0;i好像std::lock_guard在第二种情况下没有用,为什么?