草庐IT

c++ - std::errc,如何在 retval 中指示成功

我想写一个这样的C++函数:#includestd::errcf(){returnstd::errc::success;}但我无法理解如何使用std::errc'enumclass'类型返回成功值(在本例中为0)。我看到的一种方法是返回int:templateconstexprtypenamestd::underlying_type::typeto_underlying(Ee){returnstatic_cast::type>(e);}intf(){is_succ()?0:to_underlying(err);}但对我来说它看起来很丑。从标准C++0x14中的函数返回面向C的成功/错误

c++ - libstdc++ 拒绝将 volatile 右值分配给 std::ignore 是错误的吗?

我注意到libstdc++的std::ignore实现采用了constT&参数,它不能绑定(bind)到volatile右值。因此,以下代码无法编译:#include#includestructC{};usingVC=Cvolatile;intmain(){std::tuplet;std::tie(std::ignore)=std::move(t);}(http://coliru.stacked-crooked.com/a/7bfc499c1748e59e)这是否违反了标准,或者是否存在导致这种未定义行为的条款? 最佳答案 我不是语

c++ - 为什么 Visual C++ 2015 允许 std::atomic 赋值?

几天前,我写了如下内容:structA{std::atomic_boolb=false;};使用VC++2015编译器在VisualStudio2015Update3中编译,没有弹出任何错误。现在我在Ubuntu上用GCC(5.4.0)重新编译了同样的东西并得到了错误:useofdeletedfunction'std::atomic::atomic(conststd::atomic&)我在ideone上遇到了同样的错误,设置为C++14(不确定它使用的是哪个编译器版本)。当然,将代码更改为以下内容可以解决gcc的问题:structA{std::atomic_boolb{false};}

c++ - 如何捕获 I/O 异常(确切地说是 I/O,不是 std::exception)

我尝试了here中的示例程序(使用mingw-w64)。程序崩溃了。所以我编辑了它:#include//std::cerr#include//std::ifstreamintmain(){std::ifstreamfile;file.exceptions(std::ifstream::failbit|std::ifstream::badbit);try{file.open("not_existing.txt");while(!file.eof())file.get();file.close();}catch(std::ifstream::failuree){std::cerr现在它运行

c++ - 检测某些非数字类型 T 的 std::numeric::type<T> 的特化

我想检查一个类型是否在std::numeric_limits中有一个条目。当类型是一个数组时——(或者可能不是一个数字?)我得到一个编译器错误。这使我无法根据std::numeric_limits是否支持该类型来检测和分支。如果有人愿意分享任何见解,我将不胜感激。//thefollowingprovokescompilererroronClang//Functioncannotreturnarraytype'type'(aka'char[20]')static_assert(!std::numeric_limits::is_specialized,"!std::numeric_limi

c++ - C++17 中 std::unordered_map 的推导指南

我已经通过使用cppreference阅读了C++17中std::unordered_map的推导指南.然后尝试运行以下从cppreference复制的示例。#includeintmain(){//std::unordered_mapm1={{"foo",1},{"bar",2}};//Error:braced-init-listhasnotype//cannotdeducepairfrom//{"foo",1}or{"bar",2}std::unordered_mapm1=std::initializer_list>({{"foo",2},{"bar",3}});//guide#2s

c++ - std::unique 没有等价关系的例子(去掉连续的空格)

cppreference上有一个例子关于如何使用std::unique从字符串中删除连续空格:std::strings="wannagotospace?";autoend=std::unique(s.begin(),s.end(),[](charl,charr){returnstd::isspace(l)&&std::isspace(r)&&l==r;});//snowholds"wannagotospace?xxxxxxxx",where'x'isindeterminatestd::cout但是,在唯一性的要求部分中指出Elementsarecomparedusingthegiven

c++ - 在 C++ 中,是否有一种惯用的方法来防止运行操作集合导致集合发生变异的情况?

假设您有一个foo类,它包装了一些可调用对象的集合。foo有一个成员函数run(),它遍历集合并调用每个函数对象。foo还有一个成员remove(...),它将从集合中删除一个可调用对象。是否有一个惯用的、RAII风格的守卫可以放在foo.run()和foo.remove(...)中,这样删除的由对foo.run()的调用驱动会被推迟到守卫的析构函数触发?可以用标准库中的东西来完成吗?这个图案有名字吗?我当前的代码似乎不够优雅,因此我正在寻找最佳实践类型的解决方案。注意:这与并发无关。非线程安全的解决方案很好。问题在于重入和自引用。这是问题的一个例子,没有不雅的“延迟删除”守卫。cla

c++ - 在库的公共(public) API 中从 std::string、std::ostream 等过渡

对于具有相同二进制文件的多个工具链之间的API/ABI兼容性,它是wellknownthatSTL容器、std::string和其他标准库类(如iostreams)在公共(public)header中禁止。(异常(exception)情况是,如果一个人为每个版本的受支持工具链分发一个构建;一个人提供没有二进制文件的源代码供最终用户编译,这在当前情况下不是首选选项;或者一个人内联转换为其他容器,以便不同的std实现不会被库吸收。)如果一个人已经有一个不遵循此规则的已发布库API(请friend),那么最好的前进道路是什么,同时保持尽可能多的向后兼容性,并尽可能地支持编译时中断'吨?我需要

c++ - 如果 count() 是 constexpr 函数,为什么 std::array<int, count()> 不能编译?

这个问题在这里已经有了答案:constexprnotworkingifthefunctionisdeclaredinsideclassscope(3个回答)3年前关闭。为什么下面的C++代码不能用VC2017编译?structFixedMatchResults{staticconstexprstd::size_tcount(){return20;};std::arrayresults;};错误是:errorC2975:'_Size':invalidtemplateargumentfor'std::array',expectedcompile-timeconstantexpression