473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is this codepiece legal c++? and/or portable?

Hello Group,

I want to use this peace of code as
a core functionality for a polymorphic iterator.
Since i don't want to use plain new to create a
polymorphic iterator, i thought about this "workaround".
The actual storage is allocated on the stack
then inplace new constructs the object...

please tell me if the TEmbed<> is legal/valid/portable/c++?
and/or if there are other ways to do this...
By "portable" i mean: does this work on Win32(MSVC)
and Linux (GCC). I compiled it with MSVC7.1
and it worked.

template
<
typename Value,
size_t ValueSize = sizeof( Value )

class TEmbed
{
public:
typedef Value& reference;
typedef Value* pointer;

~TEmbed()
{
std::_Destroy( this->operator->() );
}

explicit TEmbed( const Value& value )
{
ASSERT( sizeof( value ) <= ValueSize );
std::_Construct( this->operator->(), value );
}

pointer operator->()
{
return reinterpret_cast< Value* >( bytes_ );
}

reference operator*()
{
return *reinterpret_cast< Value* >( bytes_ );
}

private:
char bytes_[ ValueSize ];
TEmbed();

};

// ----
struct Foo
{
Foo( int i = 0 ) : t_( i ) { }
int i_;
};

void bar()
{
typedef std::vector< Foo*> VecFooPtr;
typedef VecIFooPtr::iterator VecFooPtrIter;
typedef TEmbed< VecIFooPtrIter > EmbeddedIter;

Foo f0( 0 );
Foo f1( 1 );
Foo f2( 2 );

VecIFooPtr vec;
vec.push_back( &f0 );
vec.push_back( &f1 );
vec.push_back( &f2 );

EmbeddedIter embIterBeg( vec.begin() );
EmbeddedIter embIterEnd( vec.end() );
}
many thanks
DonBot
Jul 23 '05 #1
1 1372

"DonBot" <do****@gmx.net> skrev i en meddelelse
news:cu*************@news.t-online.com...
Hello Group,

I want to use this peace of code as
a core functionality for a polymorphic iterator.
Since i don't want to use plain new to create a
polymorphic iterator, i thought about this "workaround".
The actual storage is allocated on the stack
then inplace new constructs the object...

please tell me if the TEmbed<> is legal/valid/portable/c++?
and/or if there are other ways to do this...
By "portable" i mean: does this work on Win32(MSVC)
and Linux (GCC). I compiled it with MSVC7.1
and it worked. I see two problems:
One is the use of std::_Destroy, which I do not know. I doubt that this
function is part of the official standard, but I could of course be wrong.
Anyway why not just call the destructor directly?
The other problem is one of alignment. Your TEmbed<> class will not be
properly aligned.

/Peter


template
<
typename Value,
size_t ValueSize = sizeof( Value )

class TEmbed
{
public:
typedef Value& reference;
typedef Value* pointer;

~TEmbed()
{
std::_Destroy( this->operator->() );
}

explicit TEmbed( const Value& value )
{
ASSERT( sizeof( value ) <= ValueSize );
std::_Construct( this->operator->(), value );
}

pointer operator->()
{
return reinterpret_cast< Value* >( bytes_ );
}

reference operator*()
{
return *reinterpret_cast< Value* >( bytes_ );
}

private:
char bytes_[ ValueSize ];
TEmbed();

};

// ----
struct Foo
{
Foo( int i = 0 ) : t_( i ) { }
int i_;
};

void bar()
{
typedef std::vector< Foo*> VecFooPtr;
typedef VecIFooPtr::iterator VecFooPtrIter;
typedef TEmbed< VecIFooPtrIter > EmbeddedIter;

Foo f0( 0 );
Foo f1( 1 );
Foo f2( 2 );

VecIFooPtr vec;
vec.push_back( &f0 );
vec.push_back( &f1 );
vec.push_back( &f2 );

EmbeddedIter embIterBeg( vec.begin() );
EmbeddedIter embIterEnd( vec.end() );
}
many thanks
DonBot

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Maciej Sobczak | last post by:
Hi, I have a string. This string is to be used as a path for a new file. I would like to check if this string *could be* a valid file name, *before* I try to create the file itself. In other...
7
by: SenderX | last post by:
I am a basically a life-long Win32 C programmer specializing in high-end winsock servers. Before I dive into portable C++ programming, I wanted to code up an example of what I thought would be a...
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
8
by: Erik Cato | last post by:
Hi group! Is this code legal? typedef enum { FALSE = 0, TRUE = 1, }t_bool;
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
29
by: Ancient_Hacker | last post by:
A while back I had to recompile some old code, originally written by a really good programmer, but one prone to use every trick in the book, and then some. There was a statement, something like...
4
by: John | last post by:
6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no". Reason I'm asking is that the Ogg Vorbis source uses this pattern, and although it compiles fine on MSC and gcc 4, it does not compile...
16
by: Noob | last post by:
Hello, I've rewritten a function (greater_or_equal) that relies on implementation-defined behavior and availability of exact-width integers, with the goal of making the new implementation...
69
by: Bill Reid | last post by:
This is how I handle a check that the last character of a text file is a newline: /* checks if newline is last character of text file */ unsigned check_text_file_newline_termination(FILE...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.