我已经掌握了一些代码,我在那里看到了好奇的东西:
if (true == someFuncThatReturnsBool())
{
// Do somthing
}
bool someFuncThatReturnsBool()
{
bool retVal = false;
// .... do some stuff
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
在"if"语句中,它们用作true == someFuncThatReturnsBool()布尔表达式,对于布尔值,我通常会这样做:
if (someFuncThatReturnsBool())
{
// Do somthing
}
Run Code Online (Sandbox Code Playgroud)
如果有的话,生成的代码有什么不同?使用"true =="符号是否有任何优势,其他可能为了清楚,函数返回一个布尔值?...类型检查??,个人我看不到任何优势......
谢谢 :)
我正在搞一个测试项目,让我们称之为它mytest,它有一个 .cpp 和一个 .h 文件,内容并不重要 - 想象它包含一些简单的hello_world()类型函数......
因此,我正在制作一个通用的 makefile 将其编译到各种库输出中,其中ls -l我的输出文件夹给出:
libmytest.a
libmytest.so -> libmytest.so.1.0
libmytest.so.1 -> libmytest.so.1.0
libmytest.so.1.0
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,我的共享/静态库已创建。
现在我的 make 文件中有一个make install目标,它基本上将标头复制到/usr/local/include,并将所有这些库文件复制到/usr/local/lib
然后我制作了另一个测试 cpp 文件usertest.cpp(抱歉,名称不是很有想象力/描述性),它链接到库文件。
我通过多种方式编译:
g++ -Wall -Werror -I. -lmytestg++ -Wall -Werror -I. -lmytest -static然后我删除了 libmytest.so* 文件,所以我只有 libmytest.a 库文件然后/usr/local/lib我做了同样的测试:
g++ -Wall -Werror -I. -lmytestg++ -Wall -Werror -I. -lmytest -static最后我删除了 libmytest.a 文件并复制回 .so 文件,因此我只有 libmytest.so* 库文件然后/usr/local/lib …
我正在使用一些原子变量,所有unsigned int,我想将它们收集到一个结构中 - 实际上是一个POD.但是我也想要一个构造函数,因为我的编译器不是c ++ 11(所以我必须定义自己的构造函数来创建初始值).
所以最初我有:
// Names are not the real names - this is just for example
std::atomic<int> counter1;
std::atomic<int> counter2;
std::atomic<int> counter3;
Run Code Online (Sandbox Code Playgroud)
然后我很乐意根据需要增加/减少它们.但后来我决定再多一些计数器,因此将它们放入一个结构中:
struct my_counters {
int counter1;
int counter2;
int counter3;
// Constructor so that I can init the values I want.
my_counters(c1, c2, c3) : counter1(c1), counter2(c2), counter3(c3){;}
};
Run Code Online (Sandbox Code Playgroud)
但是因为我添加了一个自定义构造函数,所以这在技术上不再是POD.我正在阅读有关此问题的其他问题,他们说使用std :: atomic我需要一个POD,但我读到的其他问题表明结构需要是可复制的或者一些这样的...无论如何,我感到很困惑,我想要要知道我是否可以安全地使用我的结构my_counters作为原子类型:
std::atomic<my_counters> counters;
Run Code Online (Sandbox Code Playgroud)
然后在各种线程内:
// Are these operations now still atomic (and therefore safe to use across threads):
counters.counter1++;
counters.counter2--;
counters.counter3 …Run Code Online (Sandbox Code Playgroud) 因此,这可能是一个显而易见的答案,但我只是有一个疑问,因为我没有看到它的使用...
因此,使用普通的互斥锁,您可以执行以下操作:
void func1()
{
mymutex.lock();
// do func1 stuff on shared data
mymutex.unlock();
}
void func2()
{
func1(); // also uses mymutex
mymutex.lock();
// do func2 stuff on shared data
mymutex.unlock();
}
Run Code Online (Sandbox Code Playgroud)
在这里我们可以轻松地看到互斥锁部分的开始/结束。
但是使用lock_guard可以做同样的事情,即:
void func1()
{
std::lock_guard<std::mutex> g(mymutex);
// do func1 stuff on shared data
}
void func2()
{
func1(); // also uses lock_guard on mymutex
std::lock_guard<std::mutex> g(mymutex);
// do func2 stuff on shared data
}
Run Code Online (Sandbox Code Playgroud)
在这里,我的疑问是通常您使用lock_guard来保护完整的c ++块。但是在func2中,我首先调用func1(它本身使用相同的互斥锁),然后在调用func1 之后的同一块中调用lock_gaurd 。
可以/安全吗?还是func2中的lock_guard对func1有影响?
我最初以为可以,但是后来我的大脑开始告诉我这把锁可疑。我可以轻松地在func2 lock_guard周围粘贴一个块-但是现在我需要知道它是否还可以,然后我才能再次入睡:o
可以说我有代码:
main.cpp:
my_obj1 obj1("hello obj1");
my_obj2 obj2("hello obj2");
int main()
{
:
:
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否obj1总是被保证创建之前obj2。
如果我认为这两个对象在课堂上都可以,那将是正确的。
我对具有 makefile 背景的 cmake 很陌生。
我喜欢使用诸如include(cmake_utils/header.cmake)包含 cmake 文件的常见片段之类的方法,这样我就可以将它们包含在我的项目中,但只能在一处更改一次。cmake_utilsgit 仓库在哪里。
这工作得很好,但我写的每一个 CMakeLists.txt 都必须有一个cmake_minimum_required.
这很好,但我可能希望有一天改变这一点 - 假设我的一个常见文件使用了较新版本的 cmake 中的一项功能。在这种情况下,我不想更改所有 CMakeLists.txt - 我只想在一个地方更改它(理想情况下)。
这是我当前的 CMakeFile.txt:
cmake_minimum_required(VERSION 3.10.2)
# Include common elements
include(cmake_utils/header.cmake)
include(cmake_utils/cpp_flags.cmake)
# Include path
include_directories(
inc
inc/log4cpp
)
# Include source files by wild card
file(GLOB SOURCES "src/log4cpp/*.cpp")
# Setup output and libs
include(cmake_utils/output_lib_shared.cmake)
include(cmake_utils/common_libs.cmake)
Run Code Online (Sandbox Code Playgroud)
我真的想将该行cmake_minimum_required(VERSION 3.10.2)移到我的cmake_utils/header.cmake文件中。
但是当我这样做时,我在调用结束时收到以下错误cmake:
CMake Error in CMakeLists.txt:
No cmake_minimum_required command is present. A line …Run Code Online (Sandbox Code Playgroud) 我想将cppclean的输出转换为类似cppcheck的xml部分,例如:
./bit_limits.cpp:25: static data 'bit_limits::max_name_length'
变成:
<error id="static data" msg="bit_limits::max_name_length">
<location file="./bit_limits.cpp" line="25"/>
</error>
Run Code Online (Sandbox Code Playgroud)
我从awk开始:
测试代码:
echo "./bit_limits.cpp:25: static data 'bit_limits::max_name_length'" > test
cat test.out | awk -F ":" '{print "<error id=\""$3"\""}
{print "msg=\""}{for(i=4;i<=NF;++i)print ":"$i}{print "\">"}
{print "<location file=\""$1"\" line=\""$2"\"/>"}
{print "</error>"}'
Run Code Online (Sandbox Code Playgroud)
注意:要运行此cat命令,您需要将命令放回到一行中-为了便于阅读,我将其打印在多行上。
说明:
我正在使用awk冒号“:”并用冒号定界-将行分成有用的块,然后尝试将其构造成XML:
{print "<error id=\""$3"\""} -提取错误ID部分{print "msg=\""}{for(i=4;i<=NF;++i)print ":"$i}{print "\">"} -提取消息(替换缺少的冒号,这就是所有剩余的部分{print "<location file=\""$1"\" line=\""$2"\"/>"} -提取文件和行,这部分很容易,因为冒号排列很好{print "</error>"} -最后打印结束标签这很接近,但不太正确,它产生:
<error id=" static data 'bit_limits"
msg="
:
:max_name_length'
">
<location file="./bit_limits.cpp" line="25"/> …Run Code Online (Sandbox Code Playgroud) 假设我有 10 个子模块:
module/1
module/2
module/3
module/4
module/5
module/6
module/7
module/8
module/9
module/10
Run Code Online (Sandbox Code Playgroud)
module/顶级回购在哪里。
我想做git submodule foreach 'git status',但我不想为子模块 4、6 和 7 做。
有没有办法做到这一点,比如:
git submodule foreach --exclude="4 6 7" 'git status'
我尝试在命令块内使用
git submodule foreach '
if [[ $list_of_ignores =~ *"$displayname"* ]] ; then echo ignore; fi
'
Run Code Online (Sandbox Code Playgroud)
更新- 删除--exclude="4 6 7"了意外在那里
但是我收到错误说eval [[: not found- 我假设这是因为它使用 /bin/sh 而不是 /bin/bash?- 没有把握...
考虑以下代码:
#include <iostream>
#include <future>
#include <thread>
#include <chrono>
void func()
{
std::async(std::launch::async, []{std::this_thread::sleep_for(std::chrono::milliseconds(1000)); });
}
int main()
{
std::cout << "start " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() << "ms\n";
func();
std::cout << "stop " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() << "ms\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
start 18737230ms
stop 18738230ms
Run Code Online (Sandbox Code Playgroud)
我们可以看到1秒过去了才func()返回。然而,没有存储 std::future std::async(...);- 即:auto f = std::async(...)
这似乎有效 - 但我想知道它的工作机制是什么。如果我有一个 std::future (在我的小例子中是 auto f ),那么当它超出范围时,它会整理线程 - 即等待 1 秒,然后线程在幕后被处理。
进一步测试:
start 18737230ms
stop 18738230ms
Run Code Online (Sandbox Code Playgroud)
给出:
start 4448133ms
stop1 4449133ms - 1 sec …Run Code Online (Sandbox Code Playgroud) 给定结构:
struct bob
{
int a {1};
std::string b{"bob"};
};
std::vector<bob> list1{{1, "a"}, {2, "b"}, {3, "c"}};
std::vector<bob> list2{{1, "x"}, {2, "b"}, {3, "x"}};
Run Code Online (Sandbox Code Playgroud)
我想查找是否存在匹配项。执行此操作的经典方法是循环中的循环 - 类似于:
for (const auto &item1 : list1)
{
for (const auto &item2 : list2)
{
std::cout << "loop check: " << item1.a << "," << item1.b << " == "<< item2.a << "," << item2.b << "\n";
if (item2.a == item1.a &&
item2.b == item1.b)
{
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
clangtidy 现代化功能抱怨使用原始循环不是前进的方向,因此我已使用以下方法转换为 STL …