小编Cyb*_*ran的帖子

确定函数返回类型的最简单方法

给出一个非常简单但冗长的功能,例如:

int foo(int a, int b, int c, int d) {
    return 1;
}

// using ReturnTypeOfFoo = ???
Run Code Online (Sandbox Code Playgroud)

在编译时确定函数返回类型(ReturnTypeOfFoo在本例中int为:) 的最简单,最简洁的方法是什么,而不重复函数的参数类型(仅限名称,因为已知该函数没有任何额外的重载)?

c++ function return-type compile-time c++17

48
推荐指数
3
解决办法
2617
查看次数

为什么非原始类型的隐式转换没有歧义?

给定一个简单的类模板,其中包含多个隐式转换函数(非显式构造函数和转换运算符),如以下示例所示:

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)

c++ templates ambiguous implicit-conversion c++17

16
推荐指数
2
解决办法
826
查看次数

Kotlin:就地保留、替换或删除每个地图条目

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()风格的多合一解决方案,仅适用于所有条目。

java dictionary functional-programming kotlin

7
推荐指数
1
解决办法
4675
查看次数

在QT的布局中插入小部件后修复选项卡顺序

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)

c++ layout qt widget tab-ordering

5
推荐指数
1
解决办法
2487
查看次数

Apache Ant:Ant进程终止时,终止进程由&lt;exec&gt;启动

我有一个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关闭命令行窗口时运行过程蚂蚁正确终止。

windows ant cmd batch-file exec

5
推荐指数
1
解决办法
725
查看次数

C++ 17:使用泛型variadic lambda包装可调用

我想在另一个callable中透明地包装任何类型的可调用(例如lambda)以注入其他功能.包装器的类型应该与原始可调用的类型相同:

  • 相同的参数类型
  • 相同的退货类型
  • 完美转发传递的参数
  • 在SFINAE构造中使用时的行为相同

我试图使用泛型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)

c++ lambda wrapper perfect-forwarding c++17

5
推荐指数
1
解决办法
218
查看次数

在派生类中重载模板运算符

给定一个基类和一个派生类,它们都使用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。

c++ templates operator-overloading sfinae c++17

5
推荐指数
1
解决办法
153
查看次数

QFileDialog 将 MIME 类型过滤器结合到“所有格式”

我正在使用 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 类?

c++ qt openfiledialog mime-types

3
推荐指数
1
解决办法
1193
查看次数

如何在 Rust 中为相似但不同的类型重用代码?

我有一个具有一些功能的基本类型,包括特征实现:

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)

这是一个简化的例子,真正的代码会更复杂。

我想介绍两种类型,它们具有与我描述的基本类型相同的字段和行为,例如MyUserIdentifierMyGroupIdentifier。为避免在使用这些时出错,编译器应将它们视为不同的类型。

我不想复制我刚写的整个代码,我想重用它。对于面向对象的语言,我会使用继承。我将如何为 Rust 做到这一点?

code-reuse types rust

3
推荐指数
1
解决办法
327
查看次数