Skip to content

Instantly share code, notes, and snippets.

@Borgleader
Created February 2, 2014 20:55
Show Gist options
  • Save Borgleader/1c1a2005f2fab7888a31 to your computer and use it in GitHub Desktop.
Save Borgleader/1c1a2005f2fab7888a31 to your computer and use it in GitHub Desktop.
template <typename T, typename... Args>
struct ConstructorBenchmark
{
typedef typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type TStorage;
ConstructorBenchmark(Args&&... args) : args(std::forward<Args>(args)...) {}
ConstructorBenchmark(ConstructorBenchmark const& other) = delete;
ConstructorBenchmark(ConstructorBenchmark&& other) : data(std::move(other.data)), args(std::move(other.args)) {}
~ConstructorBenchmark()
{
static_cast<const T*>(static_cast<const void*>(data))->~T(); // No segfault
//static_cast<const T*>(static_cast<const void*>(&data))->~T(); // Segfault
}
void operator()() const
{
make(helper::GenerateSequence<sizeof...(Args)>{});
}
private:
template <size_t... Idxs>
void make(helper::Index<Idxs...>) const
{
new ((T*)data) T(std::get<Idxs>(args)...); // No segfault
//new ((T*)&data) T(std::get<Idxs>(args)...); // Segfault
}
TStorage data[1]; // No segfault
//TStorage data; // Segfault
std::tuple<Args...> args;
};
@Borgleader
Copy link
Author

#0 0x00007ffff7b31655 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00007ffff7b9005f in std::basic_string<char, std::char_traits, std::allocator >::~basic_string() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x0000000000409774 in ConstructorBenchmark<std::string<char const (&) [6]> >::~ConstructorBenchmark (this=0x7fffffffde10, __in_chrg=) at examples/example4.c++:27
#3 0x00000000004047a1 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at examples/example4.c++:54
#4 0x000000000040492e in _GLOBAL__sub_I__ZN6nonius17reporter_registryEv () at examples/example4.c++:54
#5 0x000000000042d2fd in __libc_csu_init ()
#6 0x00007ffff7215d75 in __libc_start_main (main=0x404545 <main(int, char**)>, argc=1, ubp_av=0x7fffffffdf78, init=0x42d2a0 <__libc_csu_init>, fini=, rtld_fini=,

stack_end=0x7fffffffdf68) at libc-start.c:219

#7 0x0000000000404049 in _start ()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment