给出一个非常简单但冗长的功能,例如:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
Run Code Online (Sandbox Code Playgroud)
在编译时确定函数返回类型(ReturnTypeOfFoo在本例中int为:) 的最简单,最简洁的方法是什么,而不重复函数的参数类型(仅限名称,因为已知该函数没有任何额外的重载)?
给定一个简单的类模板,其中包含多个隐式转换函数(非显式构造函数和转换运算符),如以下示例所示:
template<class T>
class Foo
{
private:
T m_value;
public:
Foo();
Foo(const T& value):
m_value(value)
{
}
operator T() const {
return m_value;
}
bool operator==(const Foo<T>& other) const {
return m_value == other.m_value;
}
};
struct Bar
{
bool m;
bool operator==(const Bar& other) const {
return false;
}
};
int main(int argc, char *argv[])
{
Foo<bool> a (true);
bool b = false;
if(a == b) {
// This is ambiguous
}
Foo<int> c (1);
int d = 2; …Run Code Online (Sandbox Code Playgroud) 我LinkedHashMap在 Kotlin 中有一个可变映射(具体来说是一个)。对于每个地图条目,我想有条件地执行以下三个操作之一:
在旧 Java 中,我可能会为此目的(使用或相应地)使用Iterator地图上的。但是,我想知道 Kotlin 中是否存在一种更优雅、更实用的方法来执行相同的操作(即,将 lambda 就地应用于每个条目,类似于 using ,但还可以删除条目)。entrySet()Iterator.remove()Entry.setValue()replaceAll()
在 Kotlin 中,以下组合replaceAll()和retainAll()做同样的事情:
val map = LinkedHashMap<String,Int>(mapOf("a" to 1, "b" to 2, "c" to 3))
map.replaceAll { key, value ->
if(key.startsWith("a")) {
value * 2
} else {
value
}
}
map.entries.retainAll { entry ->
entry.key.startsWith("a") || entry.key.startsWith("b")
}
Run Code Online (Sandbox Code Playgroud)
但是,这会重复映射两次,并且需要拆分谓词/转换。我更喜欢一种compute()风格的多合一解决方案,仅适用于所有条目。
我QWidget在QT 5.5中有一个自定义列表实现(的子类)。列表的元素使用进行组织QVBoxLayout。在运行时,QWidget可以在布局中的任何位置将元素(也为)动态添加到列表或从列表中删除。除一个细节外,此方法工作正常:插入的可聚焦元素的制表符顺序错误。即使插入到其他两个元素之间,最后插入的元素也始终是制表符顺序中的最后一个元素。
如何固定制表符顺序来表示布局顺序?我已经尝试遍历列表元素并setTabOrder()在每个相邻对上使用,但没有成功。
有关实现的更多详细信息:
QVBoxLayout::insertWidget() 用于插入代理窗口小部件,然后调用 QWidget::show()更新:添加了MCVE!
以下精简示例演示了该问题。为了完整起见,我还包括了标题,主函数和.pro文件。如果您不想重现此问题,则可以安全地跳过这些文件,TabOrderTestWindow.cpp是重要的问题。
TabOrderTestWindow.cpp:
#include "TabOrderTestWindow.h"
#include <QVBoxLayout>
#include <QPushButton>
// create a button inside a proxy widget
QWidget* createButtonProxy(const QString& caption, QWidget* parent) {
QWidget* proxy = new QWidget(parent);
QPushButton* button = new QPushButton(caption, proxy);
proxy->setFocusProxy(button);
return proxy;
}
TabOrderTestWindow::TabOrderTestWindow()
: QWidget()
{
setMinimumHeight(200);
setMinimumWidth(350);
QVBoxLayout* layout = new QVBoxLayout(this);
// create and add 3 buttons in order
QWidget* button1 = …Run Code Online (Sandbox Code Playgroud) 我有一个ant任务,它使用执行一个冗长的构建操作<exec>。通过Windows命令行中的批处理文件启动Ant。如果我通过关闭窗口来终止ant任务,则由开始的进程<exec>将继续运行。当ant进程本身终止时,如何实现终止生成的进程?
在Windows 7 x64和Oracle JDK 8上使用Ant 1.10.0。启动该过程的任务类似于:
<exec executable="${make.executable}" dir="${compile.dir}" failonerror="true">
<arg line="${make.parameters}" />
</exec>
Run Code Online (Sandbox Code Playgroud)
所述java关闭命令行窗口时运行过程蚂蚁正确终止。
我想在另一个callable中透明地包装任何类型的可调用(例如lambda)以注入其他功能.包装器的类型应该与原始可调用的类型相同:
我试图使用泛型variadic lambdas作为包装:
#include <iostream>
#include <type_traits>
template<class TCallable>
auto wrap(TCallable&& callable) {
return [callable = std::forward<TCallable>(callable)](auto&&... args) -> std::invoke_result_t<TCallable,decltype(args)...> {
std::cout << "This is some additional functionality" << std::endl;
return callable(std::forward<decltype(args)>(args)...);
};
}
int main(int argc, char *argv[])
{
auto callable1 = []() {
std::cout << "test1" << std::endl;
};
auto callable2 = [](int arg) {
std::cout << "test2: " << arg << std::endl;
};
auto wrapped1 = wrap(callable1);
auto wrapped2 = wrap(callable2);
static_assert(std::is_invocable_v<decltype(callable1)>); // …Run Code Online (Sandbox Code Playgroud) 给定一个基类和一个派生类,它们都使用SFINAE为特定参数类型提供了有条件启用的运算符:
#include <type_traits>
class Base
{
public:
template<class T, std::enable_if_t<std::is_scalar_v<T>>* = nullptr>
void operator>>(T& value) {
}
};
class Derived: public Base
{
public:
using Base::operator>>;
template<class T, std::enable_if_t<!std::is_scalar_v<T>>* = nullptr>
void operator>>(T& value) {
}
};
int main(int argc, char *argv[])
{
int foo;
Base base;
base >> foo; // this works
Derived derived;
derived >> foo; // this doesn't work, the operator from the base class is not considered
}
Run Code Online (Sandbox Code Playgroud)
然后,即使在适当的using Base::operator>>;声明中已经使可见的调用派生类的实例在基类中定义的运算符,也将无法工作。为什么?如何在不冗长地重复声明/定义的情况下使基类中的运算符可用?
如果所讨论的运算符不是基类中的模板,则不会发生此问题。
编辑:测试与msvc 15.9.7以及clang。
我正在使用 Qt 5.9 打开一个文件对话框,要求用户选择一个图像文件:
QStringList mimeTypeFilters;
const QByteArrayList supportedMimeTypes = QImageReader::supportedMimeTypes();
foreach(const QByteArray& mimeTypeName, supportedMimeTypes) {
mimeTypeFilters.append(mimeTypeName);
}
mimeTypeFilters.sort();
QFileDialog* fileDialog = new QFileDialog(this, "Select image");
fileDialog->setMimeTypeFilters(mimeTypeFilters);
fileDialog->setFileMode(QFileDialog::ExistingFile);
fileDialog->exec();
Run Code Online (Sandbox Code Playgroud)
所有支持的图像格式都作为 MIME 类型过滤器添加到对话框中,效果非常好。但是,我想添加一个额外的过滤器(例如“所有格式”或“所有支持的”),允许用户选择任何受支持格式的图像,因为在选择图像之前选择正确的格式非常繁琐。实现此目的的最优雅的解决方案是什么,而不需要子类化任何涉及的 Qt 类?
我有一个具有一些功能的基本类型,包括特征实现:
use std::fmt;
use std::str::FromStr;
pub struct MyIdentifier {
value: String,
}
impl fmt::Display for MyIdentifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}
impl FromStr for MyIdentifier {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(MyIdentifier {
value: s.to_string(),
})
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个简化的例子,真正的代码会更复杂。
我想介绍两种类型,它们具有与我描述的基本类型相同的字段和行为,例如MyUserIdentifier和MyGroupIdentifier。为避免在使用这些时出错,编译器应将它们视为不同的类型。
我不想复制我刚写的整个代码,我想重用它。对于面向对象的语言,我会使用继承。我将如何为 Rust 做到这一点?
c++ ×6
c++17 ×4
qt ×2
templates ×2
ambiguous ×1
ant ×1
batch-file ×1
cmd ×1
code-reuse ×1
compile-time ×1
dictionary ×1
exec ×1
function ×1
java ×1
kotlin ×1
lambda ×1
layout ×1
mime-types ×1
return-type ×1
rust ×1
sfinae ×1
tab-ordering ×1
types ×1
widget ×1
windows ×1
wrapper ×1