https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68073

--- Comment #3 from nikola.kovacs at gmail dot com ---
Never mind, I misread the documentation.

It looks like something else is causing the problem, since this works fine, and
it calls the destructor directly: (modified example from
http://en.cppreference.com/w/cpp/types/aligned_storage)

#include <iostream>
#include <type_traits>
#include <string>

template<class T>
class static_holder
{
    typename std::aligned_storage<sizeof(T), alignof(T)>::type data;

    bool allocated = false;

public:
    template<typename ...Args> void put(Args&&... args)
    {
        if (allocated) {
            reinterpret_cast<const T*>(&data)->~T();
        }
        new(&data) T(std::forward<Args>(args)...);
        allocated = true;
    }

    const T& get() const
    {
        return *reinterpret_cast<const T*>(&data);
    }

    ~static_holder()
    {
        reinterpret_cast<const T*>(&data)->~T();
    }
};

int main()
{
    static_holder<std::string> v1;
    static_holder<std::string> v2;
    v1.put("foo");
    v1.put("bar");
    v2.put("foobarbazasdfdafsdfadasdfasdf");
    v2.put("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    std::cout << v1.get() << '\n';
    std::cout << v2.get() << '\n';
}

Reply via email to