标准C++库中包含哪些异常类,它们应该用于什么?我知道有一些新的C++ 11异常,但我不确定它们是什么或它们在哪里.
[expr.new]
C++的5.3.4 2月草案给出了一个例子:
new(2,f) T[5]
导致打电话operator new[](sizeof(T)*5+y,2,f)
.这里,x和y是非负的未指定值,表示数组分配开销; new-expression的结果将从返回的值中抵消此数量
operator new[]
.这种开销可以应用于所有数组新表达式,包括那些引用库函数operator new[](std::size_t, void*)
和其他放置分配函数的表达式.开销的数量可能因新的一次调用而异.- 末端的例子 ]
现在来看以下示例代码:
void* buffer = malloc(sizeof(std::string) * 10);
std::string* p = ::new (buffer) std::string[10];
Run Code Online (Sandbox Code Playgroud)
根据上面的引用,第二行将new (buffer) std::string[10]
在内部调用operator new[](sizeof(std::string) * 10 + y, buffer)
(在构造单个std::string
对象之前).问题是如果y > 0
,预分配的缓冲区太小了!
那么我如何知道在使用数组放置时预先分配多少内存?
void* buffer = malloc(sizeof(std::string) * 10 + how_much_additional_space);
std::string* p = ::new (buffer) std::string[10];
Run Code Online (Sandbox Code Playgroud)
或者标准某处是否保证y == 0
在这种情况下?报价再次说:
这种开销可以应用于所有数组新表达式,包括那些引用库函数
operator …
新的C++有这个std :: thread类型.奇迹般有效.现在我想给每个线程一个名称以便更容易调试(比如java允许你).使用pthreads我会这样做:
pthread_setname_np(pthread_self(), "thread_name");
Run Code Online (Sandbox Code Playgroud)
但我怎么能用c ++ 0x做到这一点?我知道它在Linux系统下使用pthreads,但我想让我的应用程序可移植.有可能吗?
我听说C++有一些叫做"转换构造函数"或"转换构造函数"的东西.这些是什么,它们的用途是什么?我在这段代码中看到了它:
class MyClass
{
public:
int a, b;
MyClass( int i ) {}
}
int main()
{
MyClass M = 1 ;
}
Run Code Online (Sandbox Code Playgroud) 在std :: string的情况下,如果我们访问一个元素,其中(element position) == (size of string)
标准表示该元素返回对charT
具有value 类型的对象的引用charT()
。
const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Run Code Online (Sandbox Code Playgroud)
期望:pos <= size()。
如果pos <size(),则返回*(begin()+ pos)。否则,返回对具有值charT()的charT类型的对象的引用,在该引用中,将该对象修改为charT()以外的任何值都将导致未定义的行为。
http://eel.is/c++draft/strings#string.access-1
不幸的是,我无法对此做出解释,如果它是Undefined Behavior,那就更好了。
有人可以解释其背后的原理吗?
我不能想到在规范中也有尾调用优化的真正的RAII语言,但我知道许多C++实现可以作为特定于实现的优化来实现.
这给那些那些实现了一个问题:因为析构函数,并在自动变量的作用域结束时被调用不是由一个单独的垃圾收集例程,是不是违反TCO的约束,递归调用必须在最后的指令功能结束?
例如:-
#include <iostream>
class test_object {
public:
test_object() { std::cout << "Constructing...\n"; }
~test_object() { std::cout << "Destructing...\n"; }
};
void test_function(int count);
int main()
{
test_function(999);
}
void test_function(int count)
{
if (!count) return;
test_object obj;
test_function(count - 1);
}
Run Code Online (Sandbox Code Playgroud)
"构建......"将写入999次,然后"破坏......"再写999次.最终,test_object
在展开之前将自动分配999个实例.但假设一个实现有TCO,那么1000个堆栈帧是存在还是仅存在1?
递归调用之后的析构函数是否与事实上的TCO实现要求相冲突?
有了c ++ 11,我问自己是否在c ++ 11中替换了boost :: ptr_containers.我知道我可以使用例如a std::vector<std::unique_ptr<T> >
,但我不确定这是否完全替代.处理这些案件的推荐方法是什么?
我目前正在尝试在 C++20 下构建遗留代码库,我遇到了这样的事情:
\nsize_t someCount; // value comes from somewhere else\n\xe2\x80\xa6\nstd::vector<const char *[2]> keyValues(someCount);\n
Run Code Online (Sandbox Code Playgroud)\n我不能轻易地将其更改为类似的内容,std::vector<std:array<const char *, 2>>
因为它稍后会传递给我无法控制的某个 API。只要我不启用 C++20,上述令人厌恶的内容就可以在 Clang 和 GCC 甚至 MSVC 中正常编译,但它会在 C++20 中的 MSVC 中中断,正如您在Godbolt 中看到的那样。
我认为这与是否使用上述构造函数的DefaultInsertable要求有关(这实际上是标准强制执行的T
唯一要求)。根据 cppreference(请参阅前面的链接),C++17 之前的 STL 实现使用放置 new 来默认构造元素,从 C++20 开始,用于类型。这可能会触发 MSVC 从 C++17 回归到 C++20。std::construct_at
DefaultInsertable
该标准规定,DefaultInsertable
如果该表达式格式良好,则为类型:
allocator_traits<A>::construct(m, p)\n
Run Code Online (Sandbox Code Playgroud)\n所以就我而言,那就是:
\nconst char * dummy[2];\nusing Allocator = std::allocator<const char *[2]>;\nAllocator a;\n// …
Run Code Online (Sandbox Code Playgroud) 我编写了一个类来充当顺序容器(std::vector
/ std::queue
/ std::list
)的包装器std::map
,以便在使用少量小对象时具有a的接口.鉴于已有的算法,编码非常简单.这段代码显然是从我的完整代码高度修剪,但显示问题.
template <class key_,
class mapped_,
class traits_ = std::less<key_>,
class undertype_ = std::vector<std::pair<key_,mapped_> >
>
class associative
{
public:
typedef traits_ key_compare;
typedef key_ key_type;
typedef mapped_ mapped_type;
typedef std::pair<const key_type, mapped_type> value_type;
typedef typename undertype_::allocator_type allocator_type;
typedef typename allocator_type::template rebind<value_type>::other value_allocator_type;
typedef typename undertype_::const_iterator const_iterator;
class value_compare {
key_compare pred_;
public:
inline value_compare(key_compare pred=key_compare()) : pred_(pred) {}
inline bool operator()(const value_type& left, const value_type& right) const {return pred_(left.first,right.first);} …
Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×4
stl ×2
algorithm ×1
assert ×1
c++20 ×1
constructor ×1
exception ×1
header-files ×1
map ×1
performance ×1
raii ×1
recursion ×1
standards ×1
string ×1
unique-ptr ×1
vector ×1
visual-c++ ×1