Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
New C++ Insights Episode
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <type_traits> #include <iostream> #include <string> template< typename TInstance, typename... TArguments > inline auto StoreResource( TInstance* storage, TArguments&&... arguments ) -> std::enable_if_t<std::is_move_assignable_v<TInstance> || std::is_copy_assignable_v<TInstance>> { *storage = TInstance{ std::forward<TArguments>( arguments )... }; } template< typename TInstance, typename... TArguments > inline auto StoreResource( TInstance* storage, TArguments&&... arguments ) -> std::enable_if_t<!std::is_move_assignable_v<TInstance> && !std::is_copy_assignable_v<TInstance>> { storage->~TInstance(); storage = new( storage ) TInstance{ std::forward<TArguments>( arguments )... }; } struct Foo final { float a; float b; }; struct Bar final { float a; float b; Bar() = default; Bar( const Bar& other ) : a{ other.a }, b{ other.b } {}; Bar( Bar&& ) = delete; Bar( float a, float b ) : a{ a }, b{ b } {}; inline Bar& operator = ( const Bar& other ) { a = other.a; b = other.b; return *this; } }; struct Jiz final { float a; float b; Jiz() = default; Jiz( const Jiz& ) = delete; Jiz( Jiz&& ) = delete; Jiz( float a, float b ) : a{ a }, b{ b } {}; }; int main() { Foo foo1; Bar bar1; Jiz jiz1; StoreResource( &foo1, 3.14f, 7.83f ); std::cout << foo1.a << ' ' << foo1.b << std::endl; StoreResource( &bar1, 3.15f, 7.84f ); std::cout << bar1.a << ' ' << bar1.b << std::endl; StoreResource( &jiz1, 3.25f, 7.94f ); std::cout << jiz1.a << ' ' << jiz1.b << std::endl; return 0; }
Insight:
Console: