Skip to main content
GameDev.net gamedev.net
🔒 Locked

Why does std::aligned_storage work only with POD?

Started by Alessio1989 Sep 10, 2014 at 12:51 PM 9 replies 5.6k views
Original Post
Alessio1989
Alessio1989
For what sane reason they made this work:
auto alignValue = 16;

class Foo
{
public:
	Foo();
	~Foo();
private:
	MyMatrix matrix;
}

typedef aligned_storage< sizeof( Foo ), alignValue >::type FooAligned

But not this:
auto alignValue = 16;

class Foo
{
public:
	Foo();
	~Foo();
	
	void doSomething();

private:
	MyMatrix matrix;
}

typedef aligned_storage< sizeof( Foo ), alignValue >::type FooAligned

Just asking.... 'Cause new and delete overloading is so annoying and so '90ish sleep.png
"Recursion is the first step towards madness." - "Skegg?ld, Skálm?ld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/
BitMaster
BitMaster
From what I can see both should work. std::aligned_storage<> has no relation with the type Foo at all and you would have to do placement new/explicit destruction in both cases. What are the errors you are getting?
Alessio1989
Alessio1989
According to the internet, ccpreference.com as example, std::aligned_storage works only with PODs: http://en.cppreference.com/w/cpp/types/aligned_storage

I don't understand why it shouldn't work with structs/classes that have member function/methods.
SmkViper
SmkViper
Neither of your classes are "POD" because you don't have trivial destructors and constructors.

Adding a non-virtual function do a class does not change it's "POD-ness". "POD" is also a deprecated term in the new standard and it is preferred to talk about classes in terms of being trivial, standard-layout, or both. By extension, a "POD" is a class that is both trivial and standard-layout.

As BitMaster pointed out - need more information to determine why it isn't working.
SiCrane
SiCrane

You're misreading things. That link says std::aligned_storage<>::type is POD, it doesn't say anything about any restrictions on the type you use sizeof on to get the size.

SmkViper
SmkViper

According to the internet, ccpreference.com as example, std::aligned_storage works only with PODs: http://en.cppreference.com/w/cpp/types/aligned_storage

I don't understand why it shouldn't work with structs/classes that have member function/methods.


There is no requirement on aligned_storage and POD types - it is simply stating that aligned_storage::type IS a POD type. All aligned_storage::type is is a blank swath of memory that you can placement-new into. If you scroll down on that page you can see that their own example uses std::string which definitely is not a POD type.

Edit: Ninja'd
BitMaster
BitMaster
I checked the same reference. std::aligned_storage<> works by creating a convenient POD-type which contains uninitialized memory. The POD-type has nothing to do with Foo.

This memory can then be used to placement new construct your Foo in. If you know your Foo is std::is_trivially_copyable (you don't need the whole std::is_pod) you don't need placement new/manual destruction and can just use memcpy.

Edit: Also ninja'd.
Alessio1989
Alessio1989
OK thank you all, I learned something new today tongue.png

Anyway since I discover that MSVC12 uses 16-byte align as default, even on heap, on x86-64 platform (and not 8-byte like in x86-ia32 and ARM), I don't need it at all xD
boblehest
boblehest

OK thank you all, I learned something new today tongue.png

Anyway since I discover that MSVC12 uses 16-byte align as default, even on heap, on x86-64 platform (and not 8-byte like in x86-ia32 and ARM), I don't need it at all xD

I wouldn't make a habit of depending on implementation defined behavior like that.

Alessio1989
Alessio1989


OK thank you all, I learned something new today tongue.png

Anyway since I discover that MSVC12 uses 16-byte align as default, even on heap, on x86-64 platform (and not 8-byte like in x86-ia32 and ARM), I don't need it at all xD


I wouldn't make a habit of depending on implementation defined behavior like that.


I know, but I'am actually studying and coding only on Windows 64 and only for x86-64 platform (x86-ia32 just add to much useless restrictions and hopefully one day will remain into OSs just for compatibility with legacy applications).
SmkViper
SmkViper


OK thank you all, I learned something new today tongue.png

Anyway since I discover that MSVC12 uses 16-byte align as default, even on heap, on x86-64 platform (and not 8-byte like in x86-ia32 and ARM), I don't need it at all xD


I wouldn't make a habit of depending on implementation defined behavior like that.



While it's generally good advice to not rely on implementation defined behavior, memory alignment is one of those things you usually want the compiler to just handle for you, as it knows your target platform better then you do. If you force alignment of things to make it "portable" then at best it will have no effect and at worst you'll get terrible performance or crashes due to mis-aligned memory on some of your platforms.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.