Hello,
I'm just in the process of replacing objects that owned and deleted a pure pointer with std::unique_ptr. However, I'm running into some issues:
- I've been heavily using forward declaration to speed up compile times, like here:
class Trigger;
class Instance
{
typedef std::unordered_map<std::wstring, Trigger*> TriggerMap;
public:
~Instance(void)
{
for(auto& trigger : m_mTrigger)
{
delete trigger.second;
}
}
private:
TriggerMap m_mTrigger;
}
However, I keep getting "warning C2027" in visual studio, in that an incomplete type can't be deleted when replacing the pure pointer with std::unique_ptr. Some of the times leaving the empty dtor fixes it, however in most cases, especially containers, I have to replace the forward-declaration with a direct include in the header. Is there some way around this? It really bugs me having to do this on so many occasions, could make recompile times significantly larger (already kind of a problem since I'm heavily using templates)...
class Trigger;
class Instance
{
typedef std::unordered_map<std::wstring, std::unique_ptr<Trigger>> TriggerMap;
public:
~Instance(void); // defined in cpp-file
private:
TriggerMap m_mTrigger;
}
- I also keep getting compilation-errors with this kind of container-of-pointer setup when the class has a default copy ctor/operator. So the above example wouldn't even compile unless I put:
class Trigger;
class Instance
{
typedef std::unordered_map<std::wstring, std::unique_ptr<Trigger>> TriggerMap;
public:
Instance(void) = default;
Instance(const Instance&) = delete;
~Instance(void); // defined in cpp-file
void operator=(const Instance&) = delete;
private:
TriggerMap m_mTrigger;
}
Now I don't completely mind it, in fact I have been very sloppy about doing this before, since a class the won't compile that way shouldn't have been copyable before. Still, I wonder whether there is any shorthand to this? E.g. adding a std::unique_ptr member will automatically disable copy ctor/operator for the class, I wonder if thats possibly within a container class too?