草庐IT

c++ - 为什么 unique_ptr<T>::~unique_ptr 需要 T 的定义?

如果我有一个“酒吧”类://bar.hclassBar{public:Bar(){}};我转发声明与另一个类“Foo”中的std::unique_ptr一起使用://foo.h#includeclassBar;classFoo{public:Foo();private:std::unique_ptrbar_;};我在Foo的实现文件中包含了它的定义://foo.cpp#include"foo.h"#include"bar.h"Foo::Foo():bar_(newBar){}我收到编译时错误“‘sizeof’对不完整类型‘Bar’的无效应用”。我从here了解到和here为了解决这个问

c++ - 如何将 unique_ptr 移出 vector<unique_ptr<Foo>>?

我想搬一个unique_ptr从一个vector>.考虑我的代码:#include#include#includeusingnamespacestd;classFoo{public:intx;Foo(intx):x(x){};~Foo(){cout>();foos.push_back(unique_ptr(newFoo(100)));foos.push_back(unique_ptr(newFoo(101)));foos.push_back(unique_ptr(newFoo(102)));//Printallcoutxxx我预计它会产生:Vectorsize:3100101102Re

c++ - 在递归数据结构中 move unique_ptr<T> 数组

尝试编译以下代码会导致以下编译错误:errorC2280:'std::unique_ptr>::unique_ptr(conststd::unique_ptr>&)':attemptingtoreferenceadeletedfunction我的理解是数组“m_children”应该是可move的,因为unique_ptr指向的类型定义了move构造函数。除非这是由类的递归性质或我忽略的某些move语义元素引起的错误?#include#include#includeclassOctreeNode{public:OctreeNode(){};OctreeNode(OctreeNode&&

c++ - IEEE 754 float ,< 1 的最大数是多少?

当使用IEEE754浮点表示法(c++中的double类型)时,非常接近(可表示)整数的数字将四舍五入到最接近的整数并精确表示。是真的吗?在四舍五入之前,一个数字必须与最接近的可表示整数到底有多接近?这个距离是常数吗?例如,如果1可以精确表示,那么小于1的最大double是多少? 最佳答案 WhenusingIEEE754floatingpointrepresentation(doubletypeinc++),numbersthatareverycloseto(representable)integersareroundedtoth

C++ 中的 Javas Class<?> 等价物

所以我正在学习C++并想编写一个实体组件系统。为此,当我将组件添加到实体时,我需要知道组件的类型。在Java中,我只会做这样的事情:ClasssomeClass=myComponent.class;我可以在C++中做一些等效的事情吗?我尝试了typeid(myComponent),但在这种情况下不起作用。ExtComponent*extended=newExtComponent();Component*base=dynamic_cast(extended);std::cout这会返回“classComponent”,但我想要在这种情况下返回“classExtComponent”的东西。

c++ - 将 make_unique<X> 分配给 shared_ptr<X>

我(错误地)在我的程序中进行了以下分配:std::shared_ptrm_program;//inclassm_program=std::make_unique();//inmethod当我发现它时,我首先想知道为什么它甚至可以编译。事实证明,shared_ptr对unique_ptr对象有一个特殊的移动赋值运算符。我的问题是,这样做是否总是安全的,或者它有任何影响吗?(对于代码执行来说是安全的;对于代码审查来说显然是不安全的……) 最佳答案 从某种意义上说,这样做是“安全的”,您不会遇到任何重复删除或其他问题。这样做是不行的,因为

c++ - struct 旁边的 < > 是做什么的?

好的,下面的代码是从另一个stackoverflow问题中复制过来的heretemplatestructremove_pointer{typedefTtype;};templatestructremove_pointer{typedeftypenameremove_pointer::typetype;};虽然我知道这是模板中的递归定义,但令我困惑的是这些行templatestructremove_pointer这是否意味着remove_pointer将导致T=int*?为什么不T=int**?感谢解释。 最佳答案 这是指针类型的特化

c++ - 如何使用类似 `std::basic_istream<std::byte>` 的东西

本题旨在使用std::byte具有标准输入输出。是否有任何计划为read(_bytes)添加适当的函数重载?和write(_bytes)到basic_istream的接口(interface)和basic_ostream在未来的标准?有什么理由反对它?我知道CharT*-应保留过载。我可以做什么来使用std::byte?我目前在我的项目功能中定义std::istream&read(std::istream&,std::byte*,std::streamsize)std::ostream&write(std::ostream&,conststd::byte*,std::streamsiz

C++11:atomic<T>::store 和 atomic_store<T> 之间有什么区别

一个是模板类std::atomic的成员函数,一个是模板函数,看起来他们做的是同一件事。既然std是一个类库,为什么它同时提供类和非类版本,我认为是一样的操作?它们之间有什么真正的区别吗? 最佳答案 语义上没有区别。免费功能是为了实现与C11的源代码兼容性的尝试:#ifdef__cplusplus#include#define_Atomic(X)std::atomic#else#include#endif_Atomic(int)c;intget_c(void){returnatomic_load(&c);}

c++ - 用双花括号初始化 vector<string>

有人可以解释下例中使用双花括号和单花括号初始化的行为差异吗?代码#1:vectorv={"a","b"};stringc(v[0]+v[1]);cout输出#1:c=abc.c_str()=ab代码#2:vectorv={{"a","b"}};stringc(v[0]+v[1]);cout输出#2:c=a\acke�Z\c.c_str()=a 最佳答案 隐式转换中心。这就是正在发生的事情。vectorv={"a","b"};您可以通过提供包含两个元素的初始化列表来初始化vector。两个std::strings从字符串文字初始化,然