Thread: Question about std::aligned_storage and aliasing...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Question about std::aligned_storage and aliasing...

    I think I know the answer to this question.

    If I have a typed pointer to [code]std::aligned_storage_t<sizeof(T), alignof(T)>[array_size_], I can't do any pointer arithmetic, can I?

    By which I mean, this code is technically undefined behavior because the POD-type returned from aligned_storage makes no guarantees about the overall maximum size of each element:
    Code:
    std::aligned_storage_t<sizeof(T), alignof(T)> data[N];
    
    T* ptr = reinterpret_cast<T*>(data);
    ptr[1] = /* ... */; // oh no! This is UB!
    Am I correct in my assumptions?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't see why this would be UB.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Oh, nevermind! I read the documentation wrong!

    I thought it said that the POD type was at least the size of T but it actually at most.

    Lol my bad. I think the former case would've potentially introduced UB.

    Edit: I feel so silly now... I'm sorry I made an entire thread about this lol XD

    Btw, do you guys know of any good auto-documentation tools? I guess Doxygen is kind of the default but it's output is just okay. Granted, it's far better than what I have now...
    Last edited by MutantJohn; 05-15-2016 at 06:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strict aliasing
    By MOS-6581 in forum C Programming
    Replies: 4
    Last Post: 05-17-2014, 05:18 PM
  2. about aliasing
    By Kempelen in forum C Programming
    Replies: 1
    Last Post: 11-11-2009, 08:39 AM
  3. Anti aliasing
    By arjunajay in forum Windows Programming
    Replies: 7
    Last Post: 08-15-2006, 08:24 PM
  4. pointer aliasing
    By moi in forum C Programming
    Replies: 2
    Last Post: 02-21-2006, 11:08 AM
  5. anti aliasing
    By muttski in forum Game Programming
    Replies: 2
    Last Post: 03-16-2002, 06:55 AM

Tags for this Thread