对于这样的代码:
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) 考虑这个程序:
#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 …
我想使用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)
要使用它,我需要Zero从num_traits箱子中实现特征.但是我该如何导入Zero?一个明显的方法是添加extern crate num_traits;到我的箱子并Cargo.toml适当地修复我.但是在这样做时,我必须观察依赖的依赖性,这是不好的.
我可以以某种方式实现Zero没有明确依赖于 num_traits板条箱,如下所示?
use pathfinding::num_traits::Zero;
Run Code Online (Sandbox Code Playgroud) 我有一些实现的东西,std::iter::Iterator我想知道是否有> 0元素.这样做的标准方法是什么?count() > 0看起来太贵了.
我看到两个候选人:any(|_| true)而且nth(0).is_some(),但是我应该选择哪一个,以便未来的读者能够理解我在这里检查的内容?
我编写了适用于地图的桌面应用程序,我想对平移和长按事件做出反应.可以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的方法,我应该为鼠标编写自己的手势识别吗?
我希望得到QTapAndHoldGesture并QTapGesture在我的小部件中做出不同的事情作为对这些手势的反应.所以我重写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) 我挖了旧代码,看到了这样的函数:
inline double mod(double x, double y)
{
return x-y*floor(x/y);
}
Run Code Online (Sandbox Code Playgroud)
是fmod其全部等同,或者已经我错过了什么?
我有一个结构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) 我用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 data与const char *警告消失了,我什么也不懂,但我不希望改变结构的定义.
我对我的代码运行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[]?