/gist:e44dec26911808e18f03 Secret
Last active
August 29, 2015 13:55
-
-
Save ThePhD/e44dec26911808e18f03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
{ | |
data_ptr()->~T(); // Segfault | |
} | |
void operator()() const | |
{ | |
make(helper::GenerateSequence<sizeof...(Args)>{}); | |
} | |
private: | |
T* data_ptr () | |
{ | |
return static_cast<T*>(static_cast<void*>(&data)); | |
} | |
const T* data_ptr () const | |
{ | |
return static_cast<const T*>(static_cast<const void*>(&data)); | |
} | |
template <size_t... Idxs> | |
void make(helper::Index<Idxs...>) const | |
{ | |
new (data_ptr()) T(std::get<Idxs>(args)...); // No segfault | |
} | |
TStorage data; // Segfault | |
std::tuple<Args...> args; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment