Spionageklassen in C++
main.cpp
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>using namespace ::std::literals;
struct object
{ object()
{ ::std::clog << "constructed\n"; }
~object()
{ ::std::clog << "DESTROYED\n"; }::std::string name; };
int main()
{ ::std::clog << "start\n";{ object o;
::std::clog << "using\n"; }
::std::clog << "end\n"; }
- transcript
start
constructed
using
DESTROYED
end
—
—
—
main.cpp
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>using namespace ::std::literals;
template<typename T>
struct vector : public ::std::vector< T >
{ using ::std::vector< T >::vector;
~vector(){ ::std::clog << "destroying vector\n"s; }};struct myclass
{ static int counter;
int identity;
myclass() : identity{ ::myclass::counter++ }
{ ::std::clog << "created " <<
this->identity << " from scratch.\n"s; }myclass( myclass const & other ):
identity{ ::myclass::counter++ }
{ ::std::clog << "created " << this->identity <<
" as a copy of " << other.identity << "\n"s; }
~myclass()
{ ::std::clog << "destroying " <<
this->identity << ".\n"s; }};int ::myclass::counter = 0;
int main()
{ ::std::clog << "main 0.\n"s; vector< ::myclass > myvec;
myvec.reserve( 9 );
::std::clog << "main 1.\n"s; ::myclass c0;
::std::clog << "main 2.\n"s; ::myclass c1;
::std::clog << "main 3.\n"s; myvec.push_back( c0 );
::std::clog << "main 4.\n"s; myvec.push_back( c1 );
::std::clog << "main 5.\n"s; }transcript
main 0.
main 1.
created 0 from scratch.
main 2.
created 1 from scratch.
main 3.
created 2 as a copy of 0
main 4.
created 3 as a copy of 1
main 5.
destroying 1.
destroying 0.
destroying vector
destroying 2.
destroying 3.
—
—
—
—
—
—
—
—
—
—
—
—
static void escape( void * p )
{ asm volatile( "" : : "g"(p) : "memory" ); }struct object
{
void dump()
{ ::std::clog << " to " << static_cast< void * >( this )<< '\n'; }
object( const char * s ): name(s)
{ ::std::clog << "\n### constructing "; dump(); }
object( const object & a ): name(a.name)
{ ::std::clog << "\n### copying "; dump(); }
object( object && a ): name(std::move(a.name))
{ ::std::clog << "\n### moving "; dump(); }
object& operator +=( const object & other )
{ ::std::clog << "\n### appending " << name << ' ' <<
static_cast< void * >( this ) << " + " << other.name <<
' ' << static_cast< void const * >( &other )<< " to " <<
static_cast< void * >( this )<< '\n';name += other.name;
return *this; }
::std::string name; };