- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尽管 Position 中的 X、Y 和 Z 具有默认值,但当它在 create() 中初始化时,这些变量似乎都没有被初始化。我可以设置它们然后检索它们,但从未见过默认值。我试过以各种方式初始化它们,但没有成功。
我如何使用 std::make_unique 返回类型为 T 且设置了默认值的 unique_ptr?
#include <iostream>
#include <unordered_map>
#include <memory>
#include <typeindex>
class Component
{
public:
Component() {};
virtual ~Component() {};
};
class Position : public Component
{
public:
Position(){};
float x = 123;
float y = 321;
float z = 666;
};
std::unordered_map<std::type_index, std::unordered_map<uint32_t, std::unique_ptr<Component>>> components;
template<typename T>
T& get(uint32_t id)
{
return reinterpret_cast<T&>(components[std::type_index(typeid(T))].at(id));
}
template<typename T>
void create(uint32_t id)
{
components[std::type_index(typeid(T))].emplace(id, std::make_unique<T>());
}
int main()
{
create<Position>(8);
std::cout << get<Position>(8).z << std::endl; //Value not initialized
get<Position>(8).z;
std::cout << get<Position>(8).z << std::endl; //Still not
get<Position>(8) = Position();
std::cout << get<Position>(8).z << std::endl; //Now works, but unwanted creation of temporary Position
get<Position>(8).z = 42;
std::cout << get<Position>(8).z << std::endl; //Works
//New try
create<Position>(8);
return 0;
}
最佳答案
问题出在您的 get 方法中。如下更改它应该可以解决问题。
return reinterpret_cast<T&>(*(components[std::type_index(typeid(T))].at(id)));
你的 components[std::type_index(typeid(T))]
返回另一个 map 和 .at()
返回 std::unique_ptr
.您最初是在转换 unique_ptr
使用导致未定义行为的 reinterpret_cast。
当我们讨论这个主题时,不要使用 reinterpret_cast 进行跨层次结构的转换。使用 dynamic_cast
. dynamic_cast
当引用和指针的转换都失败时具有明确定义的行为。
简而言之,您正在做一个 reinterpret_cast<Position&>(uniquePtrToPosition)
这是非法的 C++。
关于c++ - 为什么新对象的成员没有被 std::make_unique 初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290807/
struct Foo{}; std::make_unique 之间有什么区别?和 std::make_unique> ? 最佳答案 std::make_unique创建一个 Foo具有动态存储持续时间
我正在实现循环数组数据结构,其代码如下所示: struct CircularArrayException : public std::exception { std::string msg;
全部, 我正在使用 C++14 并正在制作一个或多或少的标准单例。我正在使用最新的 Visual Studio 2017。此代码有效: #include class A { public: st
我想知道使用智能指针(如下面代码中的 Box3 )声明 Box 是否比(对我来说更经典)声明有任何优势调用构造函数(如下面代码中的 Box2)或者这两种构造之间的差异本质上是主观偏好的问题。 #inc
我在构建时不断收到以下错误, use of undeclared identifier 'make_unique' m_planet = make_unique(); 我的头文件给出了错误, #inc
我需要一个类作为延迟工厂工作,保存参数以创建另一个类并稍后及时调用 make_unique。到目前为止,我没有任何运气让可变模板版本工作。任何帮助将不胜感激(下面的最小非工作版本)。 template
https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique写道 std::make_unique 可以实现为 template st
我正在尝试为 std::unique_ptr 创建和使用 make_unique,就像 std::make_shared 存在于 std::shared_ptr described here .赫伯萨
作为 this 的后续行动发布后我想知道它的 make_unique 实现如何与分配函数临时缓冲区数组一起使用,例如以下代码。 f() { auto buf = new int[n]; // te
澄清一下,使用 make_unique 仅在表达式中有多个分配时才增加异常安全性,而不仅仅是一个,对吗?例如 void f(T*); f(new T); 是完全异常安全的(就分配和东西而言),而 vo
为什么没有std::make_unique标准 C++11 库中的函数模板?我发现 std::unique_ptr p(new SomeUserDefinedType(1, 2, 3)); 有点冗长。
考虑以下基类和派生类。 class base { public: int i{9}; virtual void func() { cout ptr{new d
在下面的代码中,有什么方法可以将参数传递给 demo使用时的构造函数 std::make_unique()分配一个 demo[]大批? class demo{ public: int info
有没有办法使用 make_unique 并将函数的输出作为参数传递? auto loadedSurface1 = std::unique_ptr(IMG_Load(path.c_str())); au
这个问题在这里已经有了答案: Template deduction for function based on its return type? (6 个答案) 关闭 4 年前。 例如我得到了这段代
make_unique 如果设计成这样,会不会更有用: template unique_ptr make_unique( ArgT ...args ) { return unique_ptr(
我遇到了 make_unique 的问题,我对此一头雾水。 _replace_find = unique_ptr(new Fl_Input{ 80, 10, 210, 25, "Find:" });
我无法找到任何关于如果将空指针传递给 std::make_unique 会发生什么的文档。 是否抛出异常? 最佳答案 std::make_unique将传递给匹配目标构造函数的所有参数转发。因此,如果
这个问题在这里已经有了答案: using c++ aggregate initialization in std::make_shared (3 个答案) 关闭 5 年前。 刚开始学习智能指针 st
我有一个 Factory 的小例子设计模式,我对这部分很感兴趣: std::make_unique(*this) ...尤其是*this . 这是否意味着 clone()方法返回 std::uniqu
我是一名优秀的程序员,十分优秀!