小编Pow*_*mer的帖子

我必须使用atomic <bool>作为"exit"bool变量吗?

我需要为另一个线程设置一个标志来退出.另一个线程会不时检查退出标志.我是否必须使用原子作为旗帜或只是一个普通的布尔就足够了(为什么(如果我使用普通布尔可能会出错的例子)?

#include <future>
bool exit = false;
void thread_fn()
{
    while(!exit)
    {
        //do stuff
        if(exit) break;
        //do stuff
    }
}
int main()
{
    auto f = std::async(std::launch::async, thread_fn);
    //do stuff
    exit = true;
    f.get();
}
Run Code Online (Sandbox Code Playgroud)

c++ atomic c++11

30
推荐指数
2
解决办法
6434
查看次数

为什么半初始化结构的 constinit 不起作用

struct A1 { int x;     int y; };
struct A2 { int x = 1; int y = 2; };
struct A3 { int x = 1; int y; };

constinit A1 a1; // x == 0, y == 0.
constinit A2 a2; // x == 1, y == 2.
          A3 a3; // x == 1, y == 0.
constinit A3 a4; // Error: illegal initialization of 'constinit' entity with a non-constant expression

int main() {}
Run Code Online (Sandbox Code Playgroud)

有什么问题a4吗?我的意思a4.x …

c++ language-lawyer c++20

14
推荐指数
1
解决办法
474
查看次数

C++ 11"枚举类型"(17.5.2.1.2)

引用C++ 11标准(17.5.2.1.2枚举类型):

1第27章中定义的几种类型是枚举类型.每个枚举类型可以实现为枚举或枚举的同义词(例如整数类型,具有常量整数值(3.9.1)).

2所述的枚举类型列举的可写为:

enum enumerated { V0 , V1 , V2 , V3 , ..... };
static const enumerated C0 (V0 );
static const enumerated C1 (V1 );
static const enumerated C2 (V2 );
static const enumerated C3 (V3 );
.....
Run Code Online (Sandbox Code Playgroud)

3这里,名称C0,C1等表示该特定枚举类型的枚举元素.所有这些元素都有不同的价值.

其中一个"枚举类型"是来自类ios_base(27.5.3 Class ios_base)的"seekdir":

// 27.5.3.1.5 seekdir
typedef T4 seekdir;
static constexpr fmtflags beg = unspecified ;
static constexpr fmtflags cur = unspecified ;
static constexpr fmtflags end = unspecified ;
Run Code Online (Sandbox Code Playgroud)

和 …

c++ enums c++11

10
推荐指数
2
解决办法
433
查看次数

为什么 std::basic_string_view 有两个相等比较运算符?

std::basic_string_view关于相等比较运算符的标准引用(请参阅http://eel.is/c++draft/string.view#comparison):

\n
\n

[示例 1:operator== 的示例一致实现如下:

\n
\n
template<class charT, class traits>\nconstexpr bool operator==(basic_string_view<charT, traits> lhs,\n                        basic_string_view<charT, traits> rhs) noexcept {\n    return lhs.compare(rhs) == 0;\n}\ntemplate<class charT, class traits>\nconstexpr bool operator==(basic_string_view<charT, traits> lhs,\n                        type_identity_t<basic_string_view<charT, traits>> rhs) noexcept {\n    return lhs.compare(rhs) == 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

\xe2\x80\x94结束示例]

\n
\n

第二个比较运算符是否足以满足所有用例?如果答案是否定的,请提供示例代码,如果删除第一个比较运算符,该代码将停止工作(或以不同方式工作)。如果答案是肯定的,那么为什么 C++ 标准明确要求定义第一个运算符?

\n

c++ c++20

10
推荐指数
1
解决办法
317
查看次数

std :: generic_category()没用?

引用C++ 11标准:

19.5.1.5错误类别对象[syserr.errcat.objects]

 const error_category& system_category() noexcept;
Run Code Online (Sandbox Code Playgroud)

4 备注:对象的equivalent虚函数应按类的规定运行error_category.对象的name虚函数应返回指向字符串的指针"system".对象的 default_error_condition虚函数应表现如下:

如果参数ev对应于POSIX errnoposv,则该函数应返回error_condition(posv, generic_category()). 否则,该函数将返回error_condition(ev, system_category()).未指定任何给定操作系统的通信内容.[ 注意:潜在的系统错误代码的数量很大且无限制,有些可能与任何POSIX错误值不对应.因此,在确定对应关系时给出了实施方式.- 结束说明 ]

换句话说,某些操作系统下面的代码可能无法正常工作,因为system_category().default_error_condition()没有正确映射到generic_category()条件(标准完全允许):

try
{
    // do some file IO
}
catch(const std::system_error& e)
{
    if(e.code() == std::errc::permission_denied) //...
}
Run Code Online (Sandbox Code Playgroud)

唯一的解决方案是实现您自己的自定义替换,以便generic_category()为您需要的所有操作系统代码(对于您需要的所有操作系统)进行映射.

enum my_errc { /*...*/, access_denied };
class my_generic_category : public std::error_category
{
    virtual bool equivalent(const error_code& …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

8
推荐指数
1
解决办法
720
查看次数

对包含unique_ptr向量的对象列表进行排序

以下代码是否应该根据C++ 11产生编译错误(如果是这样的原因?)或者它是VC11的问题?

#include <vector>
#include <list>
#include <memory>
struct A
{
    std::vector<std::unique_ptr<int>> v;
};
int main()
{
    std::list<A> l;
    l.sort([](const A& a1, const A& a2){ return true; });
}
Run Code Online (Sandbox Code Playgroud)

Visual C++ 2012产生以下编译错误:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(606): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          c:\program …
Run Code Online (Sandbox Code Playgroud)

c++ list vector unique-ptr c++11

7
推荐指数
1
解决办法
2433
查看次数

选择2 4自定义数据适配器

我试图根据这里的一个例子创建一个自定义数据适配器:http://select2.github.io/announcements-4.0.html#query-to-data-adapter.如何使用DataAdapter的定义将创建select2控件的行移动到函数外部(请参阅下面的代码)?

<!DOCTYPE html>
<head>
    <title></title>
    <link href="select2.css" rel="stylesheet" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>
    <script type="text/javascript" src="select2.full.js"></script>
    <script type="text/javascript">
        $.fn.select2.amd.require(
            ['select2/data/array', 'select2/utils'],
            function (ArrayData, Utils) {
                function CustomData ($element, options) {
                    CustomData.__super__.constructor.call(this, $element, options);
                }

                Utils.Extend(CustomData, ArrayData);

                CustomData.prototype.query = function (params, callback) {
                    var data = {results: []};
                    data.results.push({id: params.term, text: params.term});
                    data.results.push({id: 11, text: 'aa'});
                    data.results.push({id: 22, text: 'bb'});
                    callback(data);
                };

// Works if uncommented, but this line needs to be elsewhere (in $(document).ready()).
                //$("#my").select2({tags: true, dataAdapter: …
Run Code Online (Sandbox Code Playgroud)

javascript jquery-select2-4

7
推荐指数
1
解决办法
9690
查看次数

为什么路径比较在最新的文件系统草案(C++)中区分大小写?

引用编程语言 - C++ - 文件系统技术规范草案N4100:

8.4.8 pathcompare [path.compare]

1 int compare(const path&p)const noexcept;

2返回:如果*this的元素的native()在字典上比p的元素少于native(),则返回值小于0,否则如果*this的元素的native()在字典上更大,则值大于0比native()为p的元素,否则为0.

如果存在不区分大小写的文件系统(NTFS等),为什么将文件路径比较定义为区分大小写?不应该根据具体的文件系统规则进行比较吗?

c++ filesystems

6
推荐指数
1
解决办法
1246
查看次数

为什么std :: map find()没有被声明为noexcept?

C++ 14标准定义了find()成员函数,std::map如下所示:

iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
Run Code Online (Sandbox Code Playgroud)

为什么这些功能没有定义为noexcept?内部可能出错的地方,需要抛出一个异常或产生未定义的行为(除了没有找到一个元素,在这种情况下函数返回一个end迭代器并且无论如何都不需要抛出异常)?

c++ dictionary exception-specification noexcept

6
推荐指数
1
解决办法
707
查看次数

当regex_search返回true时,零匹配总是"匹配"吗?

以下是C++ 11标准的一些引用:

28.11.3 regex_search [re.alg.search]

m是一种regex_search类型的论证match_results.

2 E ff ects:确定re是否是[first,last]中与正则表达式e匹配的某个子序列.参数标志用于控制表达式与字符序列的匹配方式.如果存在这样的序列,则返回true,否则返回false.

3后置条件:在所有情况下m.ready()== true.如果函数返回false,则对参数m的影响未指定,除了m.size()返回0并且m.empty()返回true.否则,表143中给出了对参数m的影响.

表143说明了以下内容m[0].matched:

如果找到匹配则为true,否则为false.

上述似乎暗示有可能regex_search返回true,并在同一时间m[0].matchedfalse.有人可以提供一个示例(正则表达式模式和文本匹配),显示何时可能?

换句话说,什么的价值观textre下面的程序将不能断言:

#include <regex>
#include <cassert>
int main()
{
    char re[] = ""; // what kind of regular expression must it be?
    char text[] = ""; // what kind of input text must it be?
    std::cmatch m;
    assert(std::regex_search(text, m, std::regex(re)) == true);
    assert(m[0].matched == false);
}
Run Code Online (Sandbox Code Playgroud)

c++ regex c++11

4
推荐指数
1
解决办法
462
查看次数