我正在尝试定义一个具有私有结构和公共结构的类。
私有结构是仅由类使用的结构(例如 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) 我正在尝试这样的事情来使用字符串的向量列表来预填充地图。代码是不言自明的:
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 中修改它吗?
有人可以帮助我了解发生了什么。
我的编译器告诉我,我的一个成员函数有一个额外的限定错误,但我不确定为什么。
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) 以下代码S通过const &&.
然而它返回0,表明S不可移动构造!
main根据标准,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) 从文档语言中我不清楚是否必须在等待之前检查 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) 我正在努力在编译时检查类型是否为 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 做到这一点的最佳方法是什么?
我正在尝试构建一个树型结构,它将存储 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)