小编Ast*_*ngs的帖子

使用嵌套结构是否相关?

我正在尝试定义一个具有私有结构和公共结构的类。

私有结构是仅由类使用的结构(例如 SNode),我不希望它在类外可见,因为我不希望它被错误地使用(例如 new Node())。因此,我想到了将其设置为基本隐藏的想法。

公共结构是将在外部使用的结构(例如 SKeyValuePair),它将有一个指向 SNode 的指针。

代码示例如下。

[类定义]

template <typename T>
class A
{
private:

    struct SNode
    {
        SNode* pParentNode;
        SNode* pLeftChildNode;
        SNode* pRightChildNode;

        ...
    };

public:

    A<T>()
    {
    }

    virtual ~A()
    {
    }

    struct SPair
    {
    private:
    public:

        SNode* pNode;
        unsigned long long ullKey;
        T value;

        ...
    };

    const SPair GetMinKeyPair()
    {
        return SPair(...);
    }

    const SPair GetNextMinKeyPair()
    {
        ...
        return SPair(...);
    }
    };
Run Code Online (Sandbox Code Playgroud)

[用法]

A a;
...
for (A::SPair pair = a.GetMinKeyPair(); pair.pNode …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
79
查看次数

lambda 函数中的临时捕获变量 - C++11

我正在尝试这样的事情来使用字符串的向量列表来预填充地图。代码是不言自明的:

Constructor(const vector<string>& names) {
  for_each(names.begin(), names.end(),
                     [this, counter = 1](const String& choice) mutable {
                            nameMapping.emplace(choice, counter++);
                        }
  );
}
Run Code Online (Sandbox Code Playgroud)

我没有真正理解的是如何counter工作?

仅供参考:counter在 lambda 函数之外无处声明。

但是,我能够在类范围中创建一个局部变量并在可变的 lambda fn 中修改它吗?

有人可以帮助我了解发生了什么。

c++ lambda c++11

3
推荐指数
1
解决办法
226
查看次数

额外资格 C++

我的编译器告诉我,我的一个成员函数有一个额外的限定错误,但我不确定为什么。

class DigitalTime {
 public:
  void DigitalTime::intervalSince(const DigitalTime& prev, int interval) const;
};

void DigitalTime::intervalSince(const DigitalTime& prev, int interval) const {
  return;
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,它说我的函数 IntervalSince 是额外合格的。我希望你们能帮我解决这个问题

main.cpp:3:21: error: extra qualification on member 'intervalSince'
  void DigitalTime::intervalSince(const DigitalTime& aPreviousTime,
       ~~~~~~~~~~~~~^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

c++ class

3
推荐指数
1
解决办法
5140
查看次数

尽管 S 移动构造很好,为什么 std::is_move_constructible&lt;S&gt;::value == false ?什么是正确的行为?

以下代码S通过const &&.
然而它返回0,表明S不可移动构造!

  1. main根据标准,2 个标记结构中每一个的正确行为是什么?

  2. 如果返回0是正确的行为,其背后的原理是什么?
    (为什么它不应该反映类型实际上是否可以通过移动构造?)

#include <algorithm>

struct S
{
    S(          ) { }
    S(S        &) { }  // = delete; doesn't make any difference
    S(S const  &) { }  // = delete; doesn't make any difference
    S(S const &&) { }
    S(S       &&) = delete;
};

int main()
{
    S const s1;
    S s2(std::move(s1));  // OK for >= C++11
    S s3((S()));          // OK for >= C++17, …
Run Code Online (Sandbox Code Playgroud)

c++ move-constructor move-semantics c++11 c++17

3
推荐指数
1
解决办法
111
查看次数

在等待之前必须检查 std::condition_variable 谓词吗?

从文档语言中我不清楚是否必须在等待之前检查 std::condition_variable 的谓词。

cppreference上,有这样的语句:

Any thread that intends to wait on std::condition_variable has to
    1. ...
    2. check the condition, in case it was already updated and notified 
Run Code Online (Sandbox Code Playgroud)

实际上,似乎不需要检查。我只是担心如果不这样做的话会出现未定义的行为。

我关心的情况是消费者在生产者之后上线(即条件变量在另一个线程开始等待之前已被一个线程通知):

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>

int main() {
  std::condition_variable condition_var;
  std::mutex mutex;
  std::vector<std::thread> threads;
  bool flag = false;

  // Producer
  threads.emplace_back([&]() {
    {
      std::lock_guard<std::mutex> lock(mutex);
      flag = true;
      printf("%s\n", "flag = true");
    }
    condition_var.notify_all();
  });

  // Consumer
  threads.emplace_back([&]() {
    std::this_thread::sleep_for(std::chrono::seconds(3));
    { …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex c++11 c++14

2
推荐指数
1
解决办法
1360
查看次数

访问用户定义的析构函数已启动但未完成的对象真的是UB吗?

这个问题是由于Reddit的讨论引起的,一位用户告诉我,引用了标准关于对象生命周期的规则:

我很确定在技术上访问一个对象是 UB,而它正在被破坏。

例如,我依赖于管理后台线程的类;我让他们的析构函数通知线程退出并等待它退出,并且该线程可以访问该对象。我需要重构我的代码吗?

c++ object-lifetime

2
推荐指数
1
解决办法
74
查看次数

如何检查类型是否是 std::bitset 的特化?

我正在努力在编译时检查类型是否为 std::bitset 。

我想这样做:

is_bitset<std::bitset<2>>::value; // should evaluate to true
is_bitset<int>::value; // should evaluate to false
Run Code Online (Sandbox Code Playgroud)

我认为这篇SO 帖子指向了正确的方向,但由于某种原因,我无法使其与 std::bitset 一起使用。

用 C++14 做到这一点的最佳方法是什么?

c++ templates type-traits

0
推荐指数
1
解决办法
78
查看次数

为什么 valgrind 谈论 'Mismatched free()'

我正在尝试构建一个树型结构,它将存储 IMAP 命令的令牌。我正在尝试向它们添加字符串并释放它们。但是 valgrind 抱怨,我不知道为什么。

#include <iostream>
#include <algorithm>
#include <vector>

#include <string.h>

typedef enum : uint8_t
{
    TT_STRING,
    TT_INT32,
    TT_INT64,
    TT_CHAR,
    TT_PAIR
} TokenType;

typedef struct
{
    int32_t p_From;
    int32_t p_To;
} Pair;

struct Token
{
    union {
        char *t_String;
        int32_t t_Int32;
        int64_t t_Int64;
        char t_Char;
        Pair t_Pair;
    };
    TokenType t_Type;
    std::vector<Token> t_Children;
};

typedef struct Token Token;

void _token_free(Token &token)
{
    if (token.t_Type == TT_STRING)
    {
        delete token.t_String;
    }

    for_each(token.t_Children.begin(), token.t_Children.end(), [=](Token &t){
        _token_free(t);
    });
}

Token _token_str_new(const …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
76
查看次数