 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
francisrammeloo@hotmail.c Guest
|
Posted: Tue Aug 30, 2005 10:26 pm Post subject: Need some help constructing "variant" type |
|
|
Hi all,
I'm creating a sort of "variant" class. It's going pretty well, but I
have some issues.
----------------
This is my code:
----------------
class GValueImp{};
template< typename ValueType >
class GConcreteValueImp : public GValueImp
{
public:
GConcreteValueImp( ValueType inRawValue )
: mRawValue( inRawValue ){}
operator ValueType()
{ return mRawValue;}
private:
ValueType mRawValue;
};
class GValue
{
public:
template<typename ValueType>
GValue( const ValueType& inRawValue )
: mValueImp( new GConcreteValueImp<const ValueType>( inRawValue )
){}
template< typename ValueType >
operator ValueType()
{
GConcreteValueImp<ValueType>* theConcreteValueImp
= static_cast< GConcreteValueImp< ValueType >* >( mValueImp );
return *theConcreteValueImp;
}
private:
GValueImp* mValueImp;
};
----------------------
Here is some testcode:
----------------------
GValue theString( string( "abc" ) );
string s = theString;
cout << s << endl;
GValue theInt( 5 );
int i = theInt;
cout << i << endl;
----------
Output is:
----------
----------
Questions:
----------
So it works! I'm starting to feel like an expert already!
But I think it's obvious that there are some issues:
"GValue::operator ValueType" performs a static_cast:
How can I check if the cast is correct ?
What should I do if the user tries to convert to the wrong type ?
I know that dynamic_cast checks if the cast has succeeded,
but I am not allowed to use it at work. Should I try to
convince my boss to allow it for this case?
Hoping for your most construtive feedback,
BR
Francis Rammeloo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marc Mutz Guest
|
Posted: Wed Aug 31, 2005 8:40 am Post subject: Re: Need some help constructing "variant" type |
|
|
[email]francisrammeloo (AT) hotmail (DOT) com[/email] wrote:
<snip>
| Quote: | I'm creating a sort of "variant" class.
snip |
What's wrong with Boost.Any and Boost.Variant?
Marc
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
francisrammeloo@hotmail.c Guest
|
Posted: Wed Aug 31, 2005 10:41 am Post subject: Re: Need some help constructing "variant" type |
|
|
Personally I'm strongly in favor of using Boost, but at work we are not
allowed to used it. We even weren't allowed to use STL up until a year
ago. (This is because all code has to compile on Windows, Mac, Linux
and AIX systems, and software direction worries about compatibility).
Greetings,
Francis
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Aug 31, 2005 9:03 pm Post subject: Re: Need some help constructing "variant" type |
|
|
[email]francisrammeloo (AT) hotmail (DOT) com[/email] wrote:
| Quote: | Personally I'm strongly in favor of using Boost, but at work we are not
allowed to used it. We even weren't allowed to use STL up until a year
ago. (This is because all code has to compile on Windows, Mac, Linux
and AIX systems, and software direction worries about compatibility).
|
Is there anyone at your work that thinks Boost won't work on all those
platforms?
If so (and if you can't change their minds):
Download Boost to your machine, find the source code to Boost::Any
and/or Boost::Variant, copy it into your own header file(s).
Examine the code for anything that won't work on Windows, Mac, Linux
and AIX (you probably won't find any). Assuming all is well,
change the namespace, then change the comments to say that you
"cloned" the code from Boost in 2005, instead of saying that it is
part of Boost (since you're now using a different namespace).
Voila.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marc Mutz Guest
|
Posted: Wed Aug 31, 2005 9:03 pm Post subject: Re: Need some help constructing "variant" type |
|
|
[email]francisrammeloo (AT) hotmail (DOT) com[/email] wrote:
<snip>
| Quote: | Personally I'm strongly in favor of using Boost, but at
work we are not allowed to used it.
|
What a stupid policy... But I know this sentiment. Try
committing boost-using code in KDE Only solution is to
work around the stupid parts of policies, see below.
| Quote: | We even weren't
allowed to use STL up until a year ago. (This is because
all code has to compile on Windows, Mac, Linux and AIX
systems, and software direction worries about
compatibility).
snip |
At least it's not a case of NIH. You might yet survive :)
A good trick is to sneak in a boost-copy into your source
repository, let every platform team pound on it to make
it work in all your settings, and send back the changes
to the boost maintainers. This sounds like a lot of work,
but if you do it right, you've delegated maintainance of
the functionality from your team to the boost maintainers
(overlap allowed!). The alternative is to code up your
own (very likely very buggy) clones of boost libraries
and maintain them forever yourself.
When usage of boost comes up next time, you can say: "But
boss, we're using it since the release last year. It's
cost us some x to make it work on all of our platforms,
but it saved us about y (where y is usually much larger
than x, of course) of dev time had we cloned the
functionality. The next boost release will contain all
our fixes. We may as well use that directy, no? That will
save us another z, b/c we don't need to maintain our
internal boost-copy anymore."
In my experience, most boost libraries are more portable
than most projects need. Esp. if you depend on
third-party libraries (and who doesn't).
Marc
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Wed Aug 31, 2005 9:09 pm Post subject: Re: Need some help constructing "variant" type |
|
|
"francisrammeloo (AT) hotmail (DOT) com" <francisrammeloo (AT) hotmail (DOT) com> writes:
| Quote: | Personally I'm strongly in favor of using Boost, but at work we are not
allowed to used it. We even weren't allowed to use STL up until a year
ago. (This is because all code has to compile on Windows, Mac, Linux
and AIX systems, and software direction worries about compatibility).
|
I know Boost works (or worked) on all those platforms. Unfortunately we haven't
had anyone testing AIX for this release, but you can see the results
for the others here:
http://engineering.meta-comm.com/boost-regression/1_33_0/user/summary_release.html
If you wanted to run the regression script on AIX for the next release
It very well might be supported.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Sep 01, 2005 9:45 am Post subject: Re: Need some help constructing "variant" type |
|
|
Marc Mutz wrote:
| Quote: | francisrammeloo (AT) hotmail (DOT) com wrote:
snip
Personally I'm strongly in favor of using Boost, but at work
we are not allowed to used it.
What a stupid policy...
|
It depends on the reasons. One of the compilers I have to
support is Sun CC 5.1, which effectively means that I'm not
allowed to use Boost.
| Quote: | But I know this sentiment. Try committing boost-using code in
KDE
|
I believe that KDE is supposed to compile using Sun CC.
| Quote: | Only solution is to work around the stupid parts of policies,
see below.
We even weren't allowed to use STL up until a year
ago. (This is because all code has to compile on Windows,
Mac, Linux and AIX systems, and software direction worries
about compatibility).
|
I think that Boost does work on all of the platforms there
(although it obviously depends on the compilers you use).
The real question is how software direction makes such
decisions. If they establish some sort of validation process
(which could include requiring the code to work with some older
compilers, or requiring plusieur successive versions to work, to
ensure that it isn't just by accident), then it's a reasonable
decision, but it's also a decision which can (and should) change
with time. If it's just a vague worry, based on FUD, however...
| Quote: | snip
In my experience, most boost libraries are more portable than
most projects need.
|
Sun CC seems to be the exception. From what I can judge, the
problem is more on Sun's side than on Boost's, but that really
doesn't help me much; we use some third party libraries which
aren't available for g++ under Solaris.
| Quote: | Esp. if you depend on third-party libraries (and who doesn't).
|
Third-party libraries are precisely the problem. They tie us to
one particular compiler, and don't allow us to use the latest
version of that.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
francisrammeloo@hotmail.c Guest
|
Posted: Thu Sep 01, 2005 11:24 am Post subject: Re: Need some help constructing "variant" type |
|
|
| Quote: | The real question is how software direction makes such
decisions. If they establish some sort of validation process
(which could include requiring the code to work with some older
compilers, or requiring plusieur successive versions to work, to
ensure that it isn't just by accident), then it's a reasonable
decision, but it's also a decision which can (and should) change
with time. If it's just a vague worry, based on FUD, however...
|
Years ago (about 1999) they tried STL, but that didn't compile on all
compilers. On one platform not everything compiled, on another platform
the optimizations didn't work.
So they built their own library with strings, tables, filesystem
classes etc... Now stl is supported on all platforms and we start using
it here and there. But the entire codebase relies on the company's
library so we still use that most of the time.
Greetings,
Francis
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Fri Sep 02, 2005 4:36 pm Post subject: Re: Need some help constructing "variant" type |
|
|
[email]francisrammeloo (AT) hotmail (DOT) com[/email] wrote:
| Quote: | Hi all,
I'm creating a sort of "variant" class. It's going pretty well, but I
have some issues.
----------------
This is my code:
----------------
class GValueImp{};
template< typename ValueType
class GConcreteValueImp : public GValueImp
{
public:
GConcreteValueImp( ValueType inRawValue )
: mRawValue( inRawValue ){}
operator ValueType()
{ return mRawValue;}
private:
ValueType mRawValue;
};
class GValue
{
public:
template
GValue( const ValueType& inRawValue )
: mValueImp( new GConcreteValueImp
){}
template< typename ValueType
operator ValueType()
{
GConcreteValueImp
= static_cast< GConcreteValueImp< ValueType >* >( mValueImp );
return *theConcreteValueImp;
}
private:
GValueImp* mValueImp;
};
----------------------
Here is some testcode:
----------------------
GValue theString( string( "abc" ) );
string s = theString;
cout << s << endl;
GValue theInt( 5 );
int i = theInt;
cout << i << endl;
----------
Output is:
----------
abcabc
5
----------
Questions:
----------
So it works! I'm starting to feel like an expert already!
But I think it's obvious that there are some issues:
"GValue::operator ValueType" performs a static_cast:
How can I check if the cast is correct ?
|
This is the central question that every variant class must address in
some fashion. Here are a few possibilities:
The easiest answer is simply not to check the stored type against the
requested type. Just leave it up to the client to "know" (usually by
documentation or convention) what the stored type must be. The
shortcomings with the approach is that the client had better be right,
since there are no safeguards against a misidentified type.
Furthermore, the variant class loess some of its usefulness since it
cannot represent different types dynamically, at runtime; instead the
stored type must be set statically, at compile-time.
Another approach is to store both the type and the value in the variant
class itself. Better known as a "discriminated union", this type of
variant class provides a greater margin of safety and has the added
benefit of providing a more dynamic data container. However there is
still an unaddressed issue that your next question raises:
| Quote: | What should I do if the user tries to convert to the wrong type ?
|
It's hard for the library to know what to do in such a situation; none
of the available options seem particularly attractive. Therefore
avoiding this situation altogether would make the most sense. In other
words, require the user to specify how each possible conversion
(between a requested type and any of the variant's potential types)
will be handled (or explicitly not be handled). In fact, the Boost
variant library adopts just such an approach: if the user fails to
provide a behavior for a stored type and a requested type, the program
fails to compile.
| Quote: | I know that dynamic_cast checks if the cast has succeeded,
but I am not allowed to use it at work. Should I try to
convince my boss to allow it for this case?
|
No, you should not try to use a dynamic_cast. A dynamic_cast would only
be able to check the type of a polymorphic object. In the sample code
above, neither the std::string nor the int value 5 stored in the
variant class object has a polymorphic type. So a dynamic_cast would be
useless in detecting the stored type in either case.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|