小编Vic*_*voy的帖子

QML继承

假设我有一些具有相同行为的项目:

onActiveFocusChanged: {
    this.state = activeFocus ? "shown" : "hidden"
}

states: [
    State {
        name: "shown"
        PropertyChanges {
            target: myServersLV
            height: 27 * 3
            opacity: 1
        }
    },

    State {
        name: "hidden"
        PropertyChanges {
            target: myServersLV
            height: 0
            opacity: 0
        }
    }
]


Behavior on opacity {
    NumberAnimation { duration: 250
        easing {
            type: Easing.OutElastic
            amplitude: 0.5
            period: 2.5
        }
    }
}

Behavior on height {
    NumberAnimation { duration: 250
        easing {
            type: Easing.OutElastic
            amplitude: 0.5
            period: 2.5
        } …
Run Code Online (Sandbox Code Playgroud)

inheritance qt qml

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

C++ 11 POD结构初始化错误

我有简单的代码令人困惑的情况:

struct Item {
    size_t span{};
};

int main() {
    Item item{1}; // error is here
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在编译时我有以下错误:

test.cpp: In function ‘int main()’:
test.cpp:8:13: error: no matching function for call to ‘Item::Item(<brace-enclosed initializer list>)’
     Item i{1};
             ^
test.cpp:8:13: note: candidates are:
test.cpp:3:8: note: constexpr Item::Item()
 struct Item {
        ^
test.cpp:3:8: note:   candidate expects 0 arguments, 1 provided
test.cpp:3:8: note: constexpr Item::Item(const Item&)
test.cpp:3:8: note:   no known conversion for argument 1 from ‘int’ to ‘const Item&’
test.cpp:3:8: note: constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ initialization initializer-list c++11

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

使用临时寿命差异

我们正与朋友们就代码进行热烈的讨论:

#include <iostream>
#include <string>

using namespace std;

string getString() {
    return string("Hello, world!");
}

int main() {
    char const * str = getString().c_str();
    std::cout << str << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这段代码在g ++,clang和vc ++上产生不同的输出:

g++clang输出是一样的:

你好,世界!

但是vc++没有输出(或只是空格):

什么行为是正确的?根据临时生活,这可能是标准的变化吗?

据我所知,通过阅读IR clang++,它的工作原理如下:

store `getString()`'s return value in %1
std::cout << %1.c_str() << "\n";
destruct %1
Run Code Online (Sandbox Code Playgroud)

就个人而言,我认为gcc也是这样的(我用rvo/move verbosity测试它(自定义ctors和dtors打印到std::cout).为什么vc ++以其他方式工作?

clang = Apple LLVM版本6.1.0(clang-602.0.53)(基于LLVM 3.6.0svn)

g ++ = gcc版本4.9.2(Debian 4.9.2-10)

c++

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

如何从Rust中的向量获取参考切片?

我在API的某个地方使用了一个带有&[&A]参数的函数,但只有一个A对象向量。当我尝试通过以下语法使用此功能时

pub struct A(pub u64);

fn test(a: &[&A]){}

fn main() {
   let v = vec![A(1), A(2), A(3)];
   let a = &v[..];
   test(a);
}
Run Code Online (Sandbox Code Playgroud)

我有一个错误:

<anon>:12:9: 12:10 error: mismatched types:
 expected `&[&A]`,
    found `&[A]`
(expected &-ptr,
    found struct `A`) [E0308]
Run Code Online (Sandbox Code Playgroud)

我做了一些尝试,但没有成功:

let a = &v[&..]
Run Code Online (Sandbox Code Playgroud)

let a = &v[&A]
Run Code Online (Sandbox Code Playgroud)

我怎样才能让&[&A]Vec<A>

vector rust

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

Rust 中的可变弧

早些时候我有一个Sync + Send特点SyncMessenger

trait Messenger {
    fn send_message(&self, user_id: UserId, text: &str);
}

trait SyncMessenger: Messenger + Sync + Send {}
Run Code Online (Sandbox Code Playgroud)

它的实现:

pub struct DiscordMessenger {
    discord: Arc<Discord>, // (Discord is Sync and Send already)
}
impl Messenger for DiscordMessenger {
    fn send_message(&self, user_id: UserId, text: &str) {
        self.discord.send_message(user_id, text, false);
    }
}
impl SyncMessenger for DiscordMessenger {}
Run Code Online (Sandbox Code Playgroud)

并使用它:

struct Bot {
    messenger: Arc<SyncMessenger>,
}
impl Bot {
    pub fn new() -> Bot {
        Bot { …
Run Code Online (Sandbox Code Playgroud)

rust

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

为什么有些防锈类型提供静态方法而不是对象方法?

看看Rc接口,我发现Rcstruct有方法,但它们是没有定义的,self所以它们是静态的,但实际上并没有什么能阻止它们成为常用的对象方法.问题是为什么这样定义?例如,为什么Rc :: weak_count在表单中定义:

fn weak_count(this: &Rc<T>) -> usize
Run Code Online (Sandbox Code Playgroud)

代替:

fn weak_count(&self) -> usize
Run Code Online (Sandbox Code Playgroud)

rust

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

如何避免在特征实现中两次指定相同的类型?

我记得有一种方法可以定义一个特征,这样你就不需要这样写:

trait A<T> {
    fn f();
}

impl A<T> for T {
    fn f() {}
}
Run Code Online (Sandbox Code Playgroud)

据我记得,可以impl A<T> for T像这样缩短线路:

impl A for T {
Run Code Online (Sandbox Code Playgroud)

我不记得这样做的确切配方。我相信有一个与这种缩短相关的术语。

rust

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

初始化期间类型的歧义

我遇到过一个有趣的情况,即使指定了Rust,在初始化期间Rust也无法正确推断出值的类型.让我们从示例开始:

还行吧:

let level: log::LogLevelFilter = {
    let mut level = log::LogLevelFilter::Debug;
    if env::var("TRACE_ENABLED").is_ok() {
        level = log::LogLevelFilter::Trace;
    }
    level
};
Run Code Online (Sandbox Code Playgroud)

这不是:

let level: log::LogLevelFilter = {
    if env::var("TRACE_ENABLED").is_ok() {
        return log::LogLevelFilter::Trace
    }
    log::LogLevelFilter::Debug
};
Run Code Online (Sandbox Code Playgroud)

错误文字:

src/main.rs:26:20: 26:46 error: mismatched types:
 expected `()`,
    found `log::LogLevelFilter`
(expected (),
    found enum `log::LogLevelFilter`) [E0308]
src/main.rs:26             return log::LogLevelFilter::Trace
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:26:20: 26:46 help: run `rustc --explain E0308` to see a detailed explanation
Run Code Online (Sandbox Code Playgroud)

这有什么不对?我想我以前写过这样的代码没有任何问题.

具有类似问题的较短示例.

rust

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

不同地方不同物体的寿命相等

我有一个结构UrlShortener:

pub struct UrlShortener {
    client: hyper::Client,
}
impl UrlShortener {
    pub fn new() -> UrlShortener {
        UrlShortener {
            client: hyper::Client::new(),
        }
    }

    pub fn get(&self, url: &str) -> Result<String, Error> {
        let mut response = MyProvider.request(url, &self.client).send().unwrap();
        /// ...
    }
}
Run Code Online (Sandbox Code Playgroud)

MyProvider如下所示:

pub trait Provider {
    fn name(&self) -> &str;
    fn request(&self, url: &str, client: &hyper::Client) -> hyper::client::RequestBuilder;
}

pub struct MyProvider;
impl Provider for MyProvider {
    fn name(&self) -> &str {
        "myprovider"
    }

    fn …
Run Code Online (Sandbox Code Playgroud)

rust

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

如何在使用防护时避免互斥借用问题

我希望我的struct方法以同步方式执行.我想通过使用Mutex(游乐场)来做到这一点:

use std::sync::Mutex;
use std::collections::BTreeMap;

pub struct A {
    map: BTreeMap<String, String>,
    mutex: Mutex<()>,
}

impl A {
    pub fn new() -> A {
        A {
            map: BTreeMap::new(),
            mutex: Mutex::new(()),
        }
    }
}

impl A {
    fn synchronized_call(&mut self) {
        let mutex_guard_res = self.mutex.try_lock();
        if mutex_guard_res.is_err() {
            return
        }
        let mut _mutex_guard = mutex_guard_res.unwrap(); // safe because of check above
        let mut lambda = |text: String| {
            let _ = self.map.insert("hello".to_owned(),
                                    "d".to_owned());
        };
        lambda("dd".to_owned());
    }
} …
Run Code Online (Sandbox Code Playgroud)

mutex rust

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

标签 统计

rust ×7

c++ ×2

c++11 ×1

inheritance ×1

initialization ×1

initializer-list ×1

mutex ×1

qml ×1

qt ×1

vector ×1