小编sth*_*m58的帖子

两个单独的"如果",相同的条件 - 可以吗?

您认为哪两个更好?

if (the_condition)
{
    variable = sth;
}
else
{
    variable = sth_else;
}

if (the_condition)
{
    variable_2.doSth();
}
else
{
    variable_2.doSthElse();
}
Run Code Online (Sandbox Code Playgroud)

要么

if (the_condition)
{
    variable = sth;
    variable_2.doSth();
}
else
{
    variable = sth_else;
    variable_2.doSthElse();
}
Run Code Online (Sandbox Code Playgroud)

我问它,因为第二个例子显然更短.另一方面,在第一个示例中,对不同变量的操作是分开的,因此可能更容易阅读.

你认为他们中的任何一个更好吗?或者提出这样一个问题毫无意义,因为它无关紧要?

c++ syntax

22
推荐指数
4
解决办法
1581
查看次数

什么是"对象文件"中的"对象",为什么这样称呼?

我被问到一个问题:"什么是'目标文件'?".

看完Wiki后,我只知道它包含了对象.

但那些对象是什么以及为什么有人这样称呼他们呢?

c c++ linker compilation object-files

18
推荐指数
4
解决办法
8152
查看次数

如果没有正确的wait()调用关闭应用程序,QThread会发生什么?

在下面的示例中(在Qt GUI应用程序中)启动了一个新线程(带有一个事件循环,我希望在其中完成一些工作):

void doWork()
{
   QThread* workerThread = new QThread();

   Worker* worker = new Worker();
   worker->moveToThread(workerThread);

   connect(workerThread, SIGNAL(started()), worker, SLOT(startWork()));
   connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));

   connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
   connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));

   workerThread->start();
}
Run Code Online (Sandbox Code Playgroud)

startWork() 可以是一个长时间运行的操作,在此期间可以关闭应用程序.

我希望只要在应用程序startWork()上执行应用程序就不会关闭workerThread.但是,当我关闭最后一个应用程序窗口时,它workerThread会立即消失(在长时间运行期间)并且应用程序关闭没有问题.

问题出现了:

  1. 为什么立即workerThread擦拭?
    • 这是一些父母/子女问题吗?
    • Qt如何处理这种情况?
  2. 这是程序员的错误,而不是调用wait()QThread(最终)?
    • 即使这样,我也试着wait()进入一个插槽,aboutToQuit()并且在长时间运行操作完成后应用程序没有关闭(如上所述设置).仅quit(); wait();(在提到的插槽内)允许应用程序关闭.为什么?

c++ qt multithreading worker qthread

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

Concepts TS中是否有任何预定义的概念?

'Concepts lite'已被接受为TS和(示例实现)合并到GCC主分支中,因此后续问题是任何概念都是预定义的(如SortableRandom_access_range)?

我在哪里寻找这样的预定义概念?

的列表中cppreference.com的acurate的和详尽的清单?

我可以将它们与最新的GCC主干版本一起使用吗?



编辑1:由于概念未被C++ 17接受,因此将C++ 17更改为TS.

c++ stl c++-concepts c++17

3
推荐指数
2
解决办法
577
查看次数

QVariant的访客模式(无需手动型式测试和铸造)

Qt的QVariant类是否有任何现有(且方便)的访问者模式实现?

如果没有,是否有可能实现类似的东西boost::apply_visitor(),即最小化测试类型和铸造的重复?

我希望通过以下方式实现目标:

/* I have a QVariant that can contain anything, including user types */
QVariant variant;    

/* But in my Visitor I'm interested only in ints and QStrings (for the sake of the example) */
struct Visitor
{
   void operator()(int i) { /* do something with int */ } 
   void operator()(QString s) { /* ...or QString */ }
};

/* The question is: */
/* Can this be implemented in a generic way (without resorting to …
Run Code Online (Sandbox Code Playgroud)

c++ qt visitor visitor-pattern qvariant

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

CMake add_custom_command() POST_BUILD 生成器表达式扩展不起作用

我想在构建后运行 POST_BUILD 操作(但仅在调试配置中)。

在阅读了add_custom_command 文档可能的解决方案后,我明白我可以将我的命令“包装”到$<CONFIG:Debug>生成器表达式中(以确保它在发布模式下为“空”)。

我尝试了以下方法:

cmake_minimum_required(VERSION 3.18)

project(post-build CXX)
file(WRITE main.cxx "int main() {}")
add_executable(foo main.cxx)

add_custom_command(
  TARGET foo POST_BUILD
  COMMAND $<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo "hi there from debug build">
)
Run Code Online (Sandbox Code Playgroud)

但这给了我 CMake 配置时警告和构建时的硬故障(使用 Ninja 生成器):

(...) && "$<1:C:\Program Files\CMake\bin\cmake.exe" -E echo "hi there from debug build" >""
[build] The system cannot find the path specified.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

我尝试了许多可能的引号组合(包括转义引号):
COMMAND $<$<CONFIG:Debug>:"${CMAKE_COMMAND} …

cmake cmake-language

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