我不知道什么是最佳做法以及我应该使用什么.
以下两种方法有什么区别?
module.service(..);
Run Code Online (Sandbox Code Playgroud)
和
module.factory(..);
Run Code Online (Sandbox Code Playgroud) 我正在与嵌入式设备交换数据包,我真的希望能够在数据包定义的子字节部分使用枚举.但我无法猜测可能有效的语法,我怀疑它是不可能的,因为我无法弄清楚如何在C++中声明部分字节的子类型:
enum class communication_path_t : uint8_t {
Ethernet = 0, Wifi = 1
};
typedef struct {
communication_path_t pathByte; // works, uses one byte
// ...
// single byte split three ways
uint8_t retryCount : 3;
communication_path_t path : 3; // compile error
uint8_t deviceType : 2;
} packet_t;
Run Code Online (Sandbox Code Playgroud)
这不能编译,因为你无法将8位枚举放入3位字段.编辑确切的错误:
<anonymous struct>::path’ is too small to hold all values
of ‘enum class MyNamespace::communication_path_t’ [-Werror]
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的事情:
enum class communication_path_t : uint8_t : 3 { ...
Run Code Online (Sandbox Code Playgroud)
要么
typedef uint8_t:3 three_bit_int_t;
enum class …Run Code Online (Sandbox Code Playgroud) 我查看了prometheus-cpp中的语法示例和主要 prometheus 文档中非常相似的 go one,但我不确定我应该如何在我的 C++ 应用程序中使用类似的代码。Go 使用全局变量来保存计数器,C++ 在检测函数中使用本地引用。自动引用意味着我不能轻易地将计数器放入包装器中,但是每次我想增加一个计数器时都需要 10 行的开销是不可接受的。
天真的它看起来像这样:
void SomeClass::a_little_method() {
auto start = get_accurate_time();
// actual code that the method
// uses to do whatever it does
// in less than ten lines of code
auto& counter_family = BuildCounter()
.Name("little_method")
.Help("little method execution count and duration")
.Labels({
{"My Application", "metrics"}
})
.Register(*global_registry_pointer);
auto& call_counter = counter_family.Add({
{"calls", "count"}
});
auto& execution_timer = counter_family.Add({
{"calls", "duration"}
});
call_counter.Increment();
execution_timer.Increment(get_accurate_time() - …Run Code Online (Sandbox Code Playgroud) 我有一个驱动 cppcheck 坚果的代码片段,因为它没有看到日志调用中使用的变量。所以我得到未使用的变量和范围缩小警告:
double start = GetTimeOfDayDoubleSec(), afterDb = 0;
if (LoadFromDatabase(serials)) {
afterDb = GetTimeOfDayDoubleSec();
Cache.ResetDebugFlags(serials);
}
double end = GetTimeOfDayDoubleSec();
ZLOG_INFO("DB time %f, total %f", afterDb ? afterDb - start : 0, end - start);
Run Code Online (Sandbox Code Playgroud)
Cppcheck 说:
The scope of the variable 'afterDb' can be reduced.
Variable 'afterDb' is assigned a value that is never used.
Run Code Online (Sandbox Code Playgroud)
我无法找出抑制这两者的语法,并且手册没有帮助。分隔线、空格、逗号、冒号和分号都失败。单独的行给我“抑制不匹配”,其余的都是无效的:
//cppcheck-suppress variableScope
//cppcheck-suppress unreadVariable
//cppcheck-suppress variableScope unreadVariable
//cppcheck-suppress variableScope,unreadVariable
//cppcheck-suppress variableScope;unreadVariable
double afterDb = 0;
Failed to add suppression. Invalid id …Run Code Online (Sandbox Code Playgroud) 从这个示例项目的输出中,我看到我的对象的三个副本在我只期望一个时创建.并且只想要一个.我该怎么解决这个问题?
在我的真实代码中,ThreadedThing是一个更大/更重的类,它位于一个线程池中,我宁愿只有我真正需要的那么多.但我写了一个小演示应用程序(下面)来演示这种行为.我从增强线程样本中复制了基本代码,所以我希望它能正常工作,所以我担心这是一个C++新手的问题.
我之前编写过多线程代码,但是在Delphi而不是C++中.对于这个代码,valgrind说没有泄漏,这一切都很好,但仍然有三个对象被创建,我宁愿有一个.
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
class ThreadedThing {
public:
ThreadedThing() {
cout << "ThreadedThing created" << endl;
}
ThreadedThing(const ThreadedThing &orig) {
cout << "ThreadedThing copy created" << endl;
}
~ThreadedThing() {
cout << "ThreadedThing destroyed" << endl;
}
void operator()() {
cout << "ThreadedThing running" << endl;
sleep(2);
}
};
int main() {
std::vector < shared_ptr < boost::thread >> threads;
cout << "Started" << endl;
ThreadedThing thing;
std::shared_ptr<boost::thread> thread(new boost::thread(thing));
threads.push_back(thread);
for (std::vector < …Run Code Online (Sandbox Code Playgroud)