小编use*_*932的帖子

强enum typedef:clang bug还是c ++ 11标准的不确定性?

对于这样的代码:

typedef enum FooEnum : int FooEnum;
enum FooEnum : int { A = 1, B };
Run Code Online (Sandbox Code Playgroud)

clang(linux/7.0.0)报告没有错误[ -c -std=c++11 -pedantic],但gcc(linux/8.2.1)不编译它:

g++ -c -std=c++11 -pedantic test2.cpp
test2.cpp:1:28: error: expected ';' or '{' before 'FooEnum'
 typedef enum FooEnum : int FooEnum;
                            ^~~~~~~
test2.cpp:1:28: error: expected class-key before 'FooEnum'
test2.cpp:2:16: error: using typedef-name 'FooEnum' after 'enum'
 enum FooEnum : int { A = 1, B };
                ^~~
test2.cpp:1:28: note: 'FooEnum' has a previous declaration here
 typedef enum FooEnum : int FooEnum; …
Run Code Online (Sandbox Code Playgroud)

c++ enums language-lawyer c++11

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

nullptr 可以转换为 uintptr_t 吗?不同的编译器不同意

考虑这个程序:

#include <cstdint>
using my_time_t = uintptr_t;

int main() {
    const my_time_t t = my_time_t(nullptr);
}
Run Code Online (Sandbox Code Playgroud)

使用 msvc v19.24 编译失败:

<source>(5): error C2440: '<function-style-cast>': cannot convert from 'nullptr' to 'my_time_t'
<source>(5): note: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type
<source>(5): error C2789: 't': an object of const-qualified type must be initialized
<source>(5): note: see declaration of 't'

Compiler returned: 2
Run Code Online (Sandbox Code Playgroud)

但是 clang (9.0.1) 和 gcc (9.2.1) “吃”了这段代码,没有任何错误。

我喜欢 MSVC 的行为,但是否符合标准?换句话说,它是 clang/gcc …

c++ gcc clang visual-c++ c++11

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

如何在不明确定义项目中的新依赖项的情况下使用来自其他包的箱子?

我想使用dijkstra来自pathfinding箱子的功能:

pub fn dijkstra<N, C, FN, IN, FS>(
    start: &N, 
    neighbours: FN, 
    success: FS
) -> Option<(Vec<N>, C)> 
where
    N: Eq + Hash + Clone,
    C: Zero + Ord + Copy,
    FN: Fn(&N) -> IN,
    IN: IntoIterator<Item = (N, C)>,
    FS: Fn(&N) -> bool, 
Run Code Online (Sandbox Code Playgroud)

要使用它,我需要Zeronum_traits箱子中实现特征.但是我该如何导入Zero?一个明显的方法是添加extern crate num_traits;到我的箱子并Cargo.toml适当地修复我.但是在这样做时,我必须观察依赖的依赖性,这是不好的.

我可以以某种方式实现Zero没有明确依赖于 num_traits板条箱,如下所示?

use pathfinding::num_traits::Zero; 
Run Code Online (Sandbox Code Playgroud)

rust

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

为迭代器实现is_empty的规范方法是什么?

我有一些实现的东西,std::iter::Iterator我想知道是否有> 0元素.这样做的标准方法是什么?count() > 0看起来太贵了.

我看到两个候选人:any(|_| true)而且nth(0).is_some(),但是我应该选择哪一个,以便未来的读者能够理解我在这里检查的内容?

rust

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

QGestureEvent用于鼠标

我编写了适用于地图的桌面应用程序,我想对平移和长按事件做出反应.可以QGestureEvent在Qt/Linux/X11上使用普通鼠标吗?

我拿了Qt手势示例,它在平板电脑上工作,但没有按下鼠标左键并移动的反应(我希望应用程序将其识别为点击或滑动事件).

然后我添加到 Qt手势示例 app.setAttribute(Qt::AA_SynthesizeTouchForUnhandledMouseEvents, true); at main和这样的代码imagewidget.cpp:

void ImageWidget::mousePressEvent(QMouseEvent *e)
{
    e->ignore();
}
void ImageWidget::mouseReleaseEvent(QMouseEvent *e)
{
    e->ignore();
}
void ImageWidget::mouseMoveEvent(QMouseEvent *e)
{
    e->ignore();
}
Run Code Online (Sandbox Code Playgroud)

这段代码仍可在平板电脑上运行,但在Linux/X11上对鼠标没有任何反应.

任何在linux/x11上启用qgesture的方法,我应该为鼠标编写自己的手势识别吗?

c++ qt

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

如何在QTapAndHoldGesture之后忽略QTapGesture

我希望得到QTapAndHoldGestureQTapGesture在我的小部件中做出不同的事情作为对这些手势的反应.所以我重写QWidget::event方法并添加这样的代码:

 bool event(QEvent *event) override {
    if (event->type() == QEvent::Gesture) {
      auto g_event = static_cast<QGestureEvent *>(event);
      qDebug() << "GestureEvent BEGIN: gestures " << g_event->gestures().size() << ", active: " << g_event->activeGestures();
      if (auto g = qobject_cast<QTapGesture *>(g_event->gesture(Qt::TapGesture))) {
        g_event->accept(g);
        return true;
      }
      if (auto g = qobject_cast<QTapAndHoldGesture *>(g_event->gesture(Qt::TapAndHoldGesture))) {
        if (g->state() == Qt::GestureFinished) {
                qDebug("FINISHED!!!");
                g->setGestureCancelPolicy(QGesture::CancelAllInContext);
        }
        g_event->accept(g);
        return true;
      }
Run Code Online (Sandbox Code Playgroud)

问题是我QTapGesture最终没有被通缉QTapAndHoldGesture.

它看起来像这样:

GestureEvent BEGIN: gestures  1 , active:  (QTapGesture(state=GestureStarted,hotSpot=773.396,492.884,position=773.396,492.884))
GestureEvent …
Run Code Online (Sandbox Code Playgroud)

c++ qt touch gesture qt5

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

fmod与否fmod?

我挖了旧代码,看到了这样的函数:

inline double mod(double x, double y)
{
    return x-y*floor(x/y);
}
Run Code Online (Sandbox Code Playgroud)

fmod其全部等同,或者已经我错过了什么?

c c++ math

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

如何在处理数据流时有效地构建向量和该向量的索引?

我有一个结构Foo:

struct Foo {
    v: String,
    // Other data not important for the question
}
Run Code Online (Sandbox Code Playgroud)

我想处理数据流并将结果保存到字段中Vec<Foo>,Vec<Foo>并在字段上为此创建索引Foo::v.

我想使用a HashMap<&str, usize>作为索引,其中键将是&Foo::v,而值是该位置Vec<Foo>,但我对其他建议持开放态度.

我想尽可能快地处理数据流,这需要两次不做明显的事情.

例如,我想:

  • String每个数据流读取只分配一次
  • 不要搜索索引两次,一次检查密钥不存在,一次用于插入新密钥.
  • 不使用Rc或增加运行时间RefCell.

借用检查器不允许此代码:

let mut l = Vec::<Foo>::new();
{
    let mut hash = HashMap::<&str, usize>::new();
    //here is loop in real code, like: 
    //let mut s: String; 
    //while get_s(&mut s) {
    let s = "aaa".to_string();
    let idx: usize = match hash.entry(&s) …
Run Code Online (Sandbox Code Playgroud)

lifetime move-semantics rust borrowing

7
推荐指数
3
解决办法
936
查看次数

如何定义C结构:c-linkage和udt

我用C-ABI接口用X语言写的.我想从我的c ++程序中使用这个C-ABI.

我在main.cpp中写道:

extern "C" {
struct Foo {
  const char * const data;
  unsigned len;
};
struct Foo f(void);
}

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

并得到编译器的警告(visual c ++/15.7.5/windows 7/32bit):

(7):警告C4190:'f'指定了C-linkage,但返回与C不兼容的UDT'Foo'

(7):注意:见'Foo'的声明

这里有godbolt链接:https://godbolt.org/g/ztx1kf

在C++代码链接中读取错误:警告C4190:type指定了C-linkage,但是返回与C不兼容的UDT,但在我的情况下,我的POD结构中根本没有"c ++代码".

我怎样才能说服编译器这不是C++ struct Foo,而是C struct Foo

我尝试将其移动到单独的头文件(.h),但这没有任何改变.

如果我更换const char * const dataconst char *警告消失了,我什么也不懂,但我不希望改变结构的定义.

c++ visual-c++

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

不要临时调用QString :: operator []()

我对我的代码运行clazy并得到有关此类代码的警告:

QChar value() const
{
    if (hide_content_)
        return '\0';
    else
        return text()[0];
}
Run Code Online (Sandbox Code Playgroud)

哪里text()有这样的签名QString text() const;

警告是:

warning: Don't call QString::operator[]() on temporary
      [-Wclazy-detaching-temporary]
            return text()[0];
                   ^
Run Code Online (Sandbox Code Playgroud)

但是这是什么意思?是否可能QString在调用之前销毁临时对象operator[]

c++ qt

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

标签 统计

c++ ×7

qt ×3

rust ×3

c++11 ×2

visual-c++ ×2

borrowing ×1

c ×1

clang ×1

enums ×1

gcc ×1

gesture ×1

language-lawyer ×1

lifetime ×1

math ×1

move-semantics ×1

qt5 ×1

touch ×1