(注意:这个问题与Sublime 3中的中间按钮粘贴删除文本不同)
我在Linux上使用Sublime Text 3(但它也适用于Sublime Text 2).
正如预期的那样,当我点击鼠标中键时,它会复制粘贴以前突出显示的文本.不幸的是,它没有将光标移动到粘贴文本的末尾,这是我所知道的所有其他应用程序中的默认行为(相反,它根本不会移动光标).
有谁知道如何更改此行为(不要将光标移动到中间按钮粘贴)到预期的一个(将光标移动到粘贴文本的末尾)?
我正在开发一个项目,需要调用带有工具栏的模态窗口,以便在加载之前对某些数据进行一些处理.我需要工具栏的原因是用户有几个可以组合的不同选项.
这里显而易见的选择是模态对话框(我现在正在使用它).问题是我想要一个工具栏.这是一个两部分问题:
QDialog?(也可以在Qt Designer中执行此操作吗?)QMainWindow模态?在派生类中调用一个受保护的构造是不允许的,因为解释在这里.
接受的答案解释了,只有当类的对象是类的子对象时,才protected允许访问基类对象的成员.到现在为止还挺好.AAB
但是,为什么允许(至少在GCC 4.6.3中)调用静态保护方法?具体来说,以下编译对我来说没有任何意义,而注释行没有:
class A
{
protected:
A() {}
static A makeA() { return A(); }
};
class B: public A
{
public:
static A makeAFromB()
{
return makeA(); // compiles
// return A(); // does not compile
}
};
Run Code Online (Sandbox Code Playgroud)
从哲学上讲,构造函数非常类似于返回类对象的静态方法A,这就是为什么我在这里没有得到行为差异的原因.
我有一个foo()由互斥锁保护的函数,m它被定义为一个局部静态变量foo().我想知道foo()在bar具有静态存储持续时间的对象的析构函数中调用是否安全:
// foo.h
void foo();
// foo.cpp
#include "foo.h"
#include <mutex>
void foo() {
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
// ...
}
// bar.h
struct Bar { ~Bar(); };
extern Bar bar;
// bar.cpp
#include "bar.h"
#include "foo.h"
Bar::~Bar() { foo(); }
Bar bar;
// main.cpp
int main() {
Bar bar;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果std::mutex是可以轻易破坏的,这应该是安全的,因为bar之前会被破坏m.在GCC 5.4,Ubuntu 16.04上,调用std::is_trivially_destructible<std::mutex>::value返回true,所以至少在这个编译器中似乎没问题.任何明确的答案?
相关:关于静态和全局变量的 Google …
我正在使用以下内容src/CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)
project(foo)
add_library(foo SHARED foo.cpp)
set_target_properties(foo
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/subdir
)
Run Code Online (Sandbox Code Playgroud)
在 Windows 上,我使用以下方法构建库:
mkdir build
cd build
cmake ../src
cmake --build .
Run Code Online (Sandbox Code Playgroud)
输出文件: ~/build/Debug/foo.dll
预期输出文件: ~/build/Debug/subdir/foo.dll
我究竟做错了什么?
它在 Windows 以外的平台上运行良好,并且似乎应该根据以下文档运行:
根据cppreference.com和cplusplus.com,该函数std::erase(first, last)返回“最后一个删除元素之后的迭代器”。
但是,在完全没有被移除元素的特殊情况下,即当first == last(空范围)时,返回值是什么不清楚。截至 2020 年 1 月 19 日,上述消息来源均未提及此特殊情况。
例如,在以下代码中:
std::vector<int> v{1, 2, 3, 4};
auto it1 = v.erase(v.begin(), v.begin());
auto it2 = v.erase(v.end(), v.end());
Run Code Online (Sandbox Code Playgroud)
it1和的值是it2什么?
我有一个遵循这种模式的课程:
class Foo
{
public:
// Create a Foo whose value is absolute
Foo(int x) : other_(0), a_(x) {}
// Create a Foo whose value is relative to another Foo
Foo(Foo * other, int dx) : other_(other), a_(dx) {}
// Get the value
double x() const
{
if(other_)
return other_->x() + a_;
else
return a_;
}
private:
Foo * other_;
int a_;
};
Run Code Online (Sandbox Code Playgroud)
这些Foo对象都归Bar:
class Bar
{
public:
~Bar() { for(int i=0; i<foos_.size(); i++) delete foos_[i]; …Run Code Online (Sandbox Code Playgroud) 我经常更喜欢尽可能使用引用而不是指针,这使我的语法清晰.在这种情况下,我有一个类:
class Foo
{
public:
Foo(Bar & bar) : bar_(bar) {}
private:
Bar & bar_;
};
Run Code Online (Sandbox Code Playgroud)
operator=() 编译器为这样的类隐含地删除了,因为一旦设置了引用,它就无法更改(我可以在技术上定义我自己的不会改变bar_,但这不是必需的行为,所以我宁愿如果我尝试分配foo,编译器会抱怨.
我需要的是一个std::vector<Foo> v;.这在C++ 11之前是不可能的,因为模板参数必须是CopyAssignable.实际上,当我打电话时v.push_back(Foo(bar));,编译器会抱怨.但我觉得自C++ 11和Move语义以来可能是可能的.
我的问题是:是否有使用C++ 11的解决方法可以使构建这样的向量成为可能,或者我是否陷入这种情况并无法使用指针而不是引用?如果有一个解决方法,我非常感谢代码片段,因为我不熟悉移动语义.
我希望有一个foo导出类的共享库Foo。作为实现细节,我们假设有一个我不想公开的Foo类的私有数据成员。Detail
问题1:导出具有非导出类型的私有数据成员的类可以吗?
\n\n(我的理解:是的)
\n\n我正在编译以下代码g++ -c -fPIC -o foo.o foo.cpp:
/* foo.h */\n\n#pragma once\n#define FOO_API __attribute__((visibility("default")))\n#define FOO_LOCAL __attribute__((visibility("hidden")))\n\nclass FOO_LOCAL Detail {\n\n};\n\nclass Foo {\n Detail d_;\n};\n\n/* foo.cpp */\n\n#include "foo.h"\n\n// nothing\nRun Code Online (Sandbox Code Playgroud)\n\n在 Ubuntu 16.04 64 位上的 gcc 4.6.0 下,我收到以下警告:
\n\nfoo.h:8:7: warning: \xe2\x80\x98Foo\xe2\x80\x99 declared with greater visibility than the type of its field \xe2\x80\x98Foo::d_\xe2\x80\x99 [-Wattributes]\n class Foo {\n ^\nRun Code Online (Sandbox Code Playgroud)\n\n问题2:为什么会出现警告?
\n\n我确实知道它Foo比其数据成员的类型具有更大的可见性Foo::d_ …
(注:我已经在pybind11 的 GitHub上问过这个问题。为更广泛的受众提供支持。也标记 boost.python 以防有人有洞察力,但问题是如何使用 pybind11 做到这一点)
环境:Ubuntu 18.04、GCC 7.4.0、Python 3.6.9、pybind11 2.4.3
假设我们想用 pybind11 包装一个共享库libfoo.so,导出一个基类Foo(即,使用 vtable,在本例中只是一个虚拟析构函数)。
如果我们导出整个类(即添加visibility("default")到整个类),那么一切都会按预期进行:
CMakeLists.txt
cmake_minimum_required(VERSION 3.1.0)
add_subdirectory(pybind11)
add_library(foo SHARED foo.cpp)
set_target_properties(foo PROPERTIES CXX_VISIBILITY_PRESET hidden)
pybind11_add_module(pyfoo pyfoo.cpp)
target_link_libraries(pyfoo PRIVATE foo)
Run Code Online (Sandbox Code Playgroud)
foo.h
#define FOO_API __attribute__((visibility("default")))
class FOO_API Foo { public: virtual ~Foo(); };
Run Code Online (Sandbox Code Playgroud)
foo.cpp
#include "foo.h"
Foo::~Foo() {}
Run Code Online (Sandbox Code Playgroud)
pyfoo.cpp
#include <pybind11/pybind11.h>
#include "foo.h"
PYBIND11_MODULE(pyfoo, m) {
pybind11::class_<Foo>(m, "Foo")
.def(pybind11::init<>());
}
Run Code Online (Sandbox Code Playgroud)
测试.py
import pyfoo
f = pyfoo.Foo()
print("Test succeeded")
Run Code Online (Sandbox Code Playgroud)
构建并运行: …
为了更好地理解如何使用pybind库将参数从Python传递到C++函数,我想构建一个小的虚拟/演示代码,我可以在C++端接收Python列表,将其转换为浮点指针对象,以及然后打印出来.
虽然我知道我可以使用这个py::list类,但我还没有想出这个类可用的方法.我查看了文档参考,然后在代码(list.h,stl.h)中查找并且无法确定哪些方法可用.
相当于__getitem__什么?我有可用的每个python方法py::list吗?
当一个对象需要在没有"拥有它"的情况下引用另一个对象(即,不对其生命周期负责)时,一种方法就是使用原始指针或原始引用,就像在这个例子中一样:
class Node
{
std::vector<Edge*> incidentEdges;
};
class Edge
{
Node* startNode;
Node* endNode;
};
class Graph
{
std::vector<std::unique_ptr<Node*>> nodes;
std::vector<std::unique_ptr<Edge*>> edges;
};
Run Code Online (Sandbox Code Playgroud)
(请节省时间评论图表是否存在更有效的数据结构,这是我的专业领域,而不是问题的重点.)
Graph负责节点和边的生命周期,并负责保证指针在Node和Edge不悬空.但是如果程序员没有这样做,那么存在未定义行为的风险.
但是,由于引用计数的开销成本,人们可以强烈强制使用智能指针不会发生未定义的行为.相反,它会优雅地崩溃.它保证这可以在尽可能早的时间发生(避免破坏更多数据)并且不会被忽视.这是一个可能的实现:
(编辑:修复实施,更多细节在Yakk回答.非常感谢!)
template <class T>
using owning_ptr = std::shared_ptr<T>;
template <class T>
class nonowning_ptr
{
std::weak_ptr p_;
public:
nonowning_ptr() : p_() {}
nonowning_ptr(const nonowning_ptr & p) : p_(p.p_) {}
nonowning_ptr(const owning_ptr<T> & p) : p_(p) {}
// checked dereferencing
owning_ptr<T> get() const
{
if (auto sp = p_.lock()) …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×2
pybind11 ×2
stdvector ×2
boost-python ×1
cmake ×1
dll ×1
inheritance ×1
linux ×1
modal-dialog ×1
mutex ×1
python ×1
qdialog ×1
qmainwindow ×1
qt ×1
static ×1
sublimetext2 ×1
sublimetext3 ×1
typeinfo ×1
vector ×1
visibility ×1
vtable ×1
weak-ptr ×1
windows ×1