这是未定义的行为吗?
ptrdiff_t one() {
std::vector<int> test(1);
return &test[1] - &test[0];
}
Run Code Online (Sandbox Code Playgroud)
这是未定义的行为吗?
ptrdiff_t zero() {
std::vector<int> test;
int * end = &test[0];
int * begin = &test[0];
return end - begin;
}
Run Code Online (Sandbox Code Playgroud)
如果其中任何一个是未定义的行为,任何人都可以帮我找到C++ 11规范中的部分,它描述了向量的下标运算符必须在小于(而不是小于或等于)大小的值上调用, 或相反亦然?
谢谢
这是Swift教程的链接.在初始化 - 覆盖可用的初始化程序部分中 请注意,如果使用不可用的子类初始化程序覆盖可用的超类初始化程序,则子类初始值设定项不能委托超类初始化程序.
但是下面的例子:
class Document {
var name: String?
// this initializer creates a document with a nil name value
init() {}
// this initializer creates a document with a non-empty name value
init?(name: String) {
if name.isEmpty { return nil }
self.name = name
}
}
Run Code Online (Sandbox Code Playgroud)
和:
class AutomaticallyNamedDocument: Document {
override init() {
super.init()
self.name = "[Untitled]"
}
// This is nonfailable override superclass's failable
override init(name: String) {
// Why subclass initializer …Run Code Online (Sandbox Code Playgroud) conditional_variable 不是可复制构造、可移动构造、可复制分配、可移动分配。
我们可以这样打电话吗
vector<conditional_variable> cond;
conditional_variable c1;
conditional_variable c2;
cond.push_back(c1);
cond.push_back(c2);
Run Code Online (Sandbox Code Playgroud)
在这些情况下正确的处理方法是什么
如何更正以下代码以避免错误消息?这段代码不适用吗?是否有可以使用的不同技术(packaged_task,asynch)?
#include <list>
#include <future>
using namespace std;
template<typename T>
struct sorter
{
struct chunk_to_sort
{
std::list<T> data_m;
std::promise<std::list<T> > promise_m;
};
list<chunk_to_sort> chunks_m;
void do_sort(std::list<T>& chunk_data)
{
std::list<T> result;
result.splice(result.begin(),chunk_data,chunk_data.begin());
T const& partition_val=*result.begin();
typename std::list<T>::iterator divide_point = std::partition(chunk_data.begin(),chunk_data.end(),[&](T const& val){return val<partition_val;});
chunk_to_sort new_lower_chunk;
new_lower_chunk.data_m.splice(new_lower_chunk.data_m.end(), chunk_data,chunk_data.begin(),divide_point);
chunks_m.emplace_back(std::move(new_lower_chunk));
}
};
int main()
{
sorter<int> s;
list<int> l;
l.push_back(4);
l.push_back(3);
s.do_sort(l);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我阅读了[The C++ Standard Library Second Edition]这本书,并找到了以下部分:
namespace std {
template <typename T, typename D>
class unique_ptr<T[], D>
{
public:
typedef ... pointer; // may be D::pointer
typedef T element_type;
typedef D deleter_type;
...
};
}
Run Code Online (Sandbox Code Playgroud)
元素类型T可能是void,因此唯一指针拥有一个具有未指定类型的对象,如void*do.还要注意,定义了一个类型指针,它不一定定义为T*.如果删除器D具有指针typedef,则将使用此类型.在这种情况下,模板参数T仅具有类型标记的效果,因为没有成员作为依赖于T的类unique_ptr <>的一部分; 一切都取决于指针.优点是unique_ptr因此可以容纳其他智能指针.
在阅读本节之后,我仍然无法理解"一切都取决于指针"的目的.是否有人可以提供一些样品?谢谢.
如果我这样做,
int* p = new int(10);
std::shared_ptr<int>(p);
delete p;
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?shared_ptr删除原始指针后是无效的吗?在这种情况下,有没有办法确保内存访问安全?
我可以内联来回转换.
std::shared_ptr<sfg::Notebook> mNotebook = ...;
std::weak_ptr<sfg::Notebook> weakNotebook(mNotebook);
std::shared_ptr<sfg::Notebook> strongNotebook(weakNotebook);
Run Code Online (Sandbox Code Playgroud)
当我尝试将其传递给方法时,我收到错误:
"No viable conversion between std::weak_ptr<sfg::Notebook> to std::shared_ptr<sfg::Notebook>."
Run Code Online (Sandbox Code Playgroud)
我正常调用方法,方法如下:
onNotebookLeftClick(weakNotebook);
void onNotebookLeftClick(std::shared_ptr<sfg::Notebook> notebook) {
}
Run Code Online (Sandbox Code Playgroud)
我正在使用OS X(10.10.2).
我遇到了以下代码段
std::string&& test()
{
std::string m="Hello";
return (std::move(m));
}
int main()
{
std::string&& m = test();
}
Run Code Online (Sandbox Code Playgroud)
我理解上面的代码不正确且不安全,但我不确定原因.到目前为止,这是我对代码的理解.在功能test
在堆栈上创建一个std::string调用的局部变量m.
然后返回此字符串,但不是复制,而是将其内容移动到临时字符串.此时函数test结束调用变量的析构函数m(其内容被移动到temp)
临时值现在绑定到右值引用m.根据我的理解,一个临时的将保持活着,直到它的绑定对象存在并且在范围内.
有人可以告诉我哪里可能出错了吗?为什么上面的代码不安全?
该代码在C ++标准库中是否已定义行为?
std::forward_list<T> list;
list.erase_after(list.before_begin());
Run Code Online (Sandbox Code Playgroud)
直觉会说不,但是我无法找到这种特殊情况的确切标准措辞。
我有一个(例如)uint8_ts 的数组.
std::unique_ptr用于在内存中管理此对象的错误工具是什么?
例如,
std::unique_ptr<uint8_t> data(new uint8_t[100]);
Run Code Online (Sandbox Code Playgroud)
这会产生不确定的行为吗?
我想要一个智能指针对象来为我管理一些分配的内存.std::vector不理想,因为它是一个动态的对象.std::array也不好,因为在编译时不知道分配的大小.我无法使用[目前,2016-03-06]实验std::dynarray,因为Visual Studio 2013尚未提供.
不幸的是,我必须遵守VS2013,因为,规则.
c++ ×9
c++11 ×9
shared-ptr ×2
unique-ptr ×2
forward-list ×1
future ×1
ios ×1
promise ×1
raw-pointer ×1
stl ×1
swift ×1
vector ×1
weak-ptr ×1
xcode ×1