您认为哪两个更好?
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)
我问它,因为第二个例子显然更短.另一方面,在第一个示例中,对不同变量的操作是分开的,因此可能更容易阅读.
你认为他们中的任何一个更好吗?或者提出这样一个问题毫无意义,因为它无关紧要?
在下面的示例中(在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会立即消失(在长时间运行期间)并且应用程序关闭没有问题.
问题出现了:
workerThread擦拭?
wait()QThread(最终)?
wait()进入一个插槽,aboutToQuit()并且在长时间运行操作完成后应用程序没有关闭(如上所述设置).仅quit(); wait();(在提到的插槽内)允许应用程序关闭.为什么?'Concepts lite'已被接受为TS和(示例实现)合并到GCC主分支中,因此后续问题是任何概念都是预定义的(如Sortable或Random_access_range)?
我在哪里寻找这样的预定义概念?
是的列表中cppreference.com的acurate的和详尽的清单?
我可以将它们与最新的GCC主干版本一起使用吗?
编辑1:由于概念未被C++ 17接受,因此将C++ 17更改为TS.
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) 我想在构建后运行 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} …
c++ ×5
qt ×2
c ×1
c++-concepts ×1
c++17 ×1
cmake ×1
compilation ×1
linker ×1
object-files ×1
qthread ×1
qvariant ×1
stl ×1
syntax ×1
visitor ×1
worker ×1