Problem with type hiding (and user survival)

S

StephQ

I face two problems related to template classes.

The first one is that you can not have the parameters of a class
automatically deduced by the constructor.
You can solve this problem using make_... template functions (where
now the template arguments get correctly deduced).

However, I still have the following problem, consider:

VariateSampler<
GaussianDistribution,
MersenneTwister>
gaussianSampler( myGenerator, GaussianDistribution() );

Here GaussianDistribution and MersenneTwister are classes, myGenerator
is of type MersenneTwister.

I would really like to write:
VariateSampler<> gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );
VariateSampler<> poissonSampler = make_VariateSampler( myGenerator,
PossionDistribution() );

resulting in gaussianSampler and poissonSampler to be of different
types (deduced from the rhs):

gaussianSampler of type
VariateSampler<MersenneTwisterPseudoRng,GaussianDistribution>

and poissonSampler of type
VariateSampler<MersenneTwisterPseudoRng,PoissonDistribution>

These types are not complicated, but it is only because I simplified
the example, in the real code they are absolutely intractable (no user
could ever write the correct types by hand!).

One solution could be to pass the result of make_... directly where
needed (to a class expecting a template parameter as argument of
constructor), but sometimes I need to pass the same object to two
classes, so this approach is not feasible.

If I use boost::any then I need the exact type of the wrapped object
to cast back to the original type, and I would be back to the original
problem!

And I can not use the type() member function (that returns the type of
the wrapped object) to get back the type of the wrapped object (so
that I correctly always cast to the right stuff) because then I would
be passing a run-time decided type as template parameter. :(

The main question is: why is not there a C++ keyword like:

static_any gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );

that means that gaussianSampler is the type of RHS (hence avoiding the
user the pain to actually write the extremely complicated type of the
RHS) ?
The compiler is able to check if the type on the RHS is decided on
compile time or not, and give error if it is not.
Is it possible to implement something like this in the current C++
language?

I would really appreciate any help on this point, it is another
recurring problem in my library.
Thank you in advance.

Best Regards
StephQ
 
A

Alf P. Steinbach

* StephQ:
be passing a run-time decided type as template parameter. :(
The main question is: why is not there a C++ keyword like:

static_any gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );

that means that gaussianSampler is the type of RHS (hence avoiding the
user the pain to actually write the extremely complicated type of the
RHS) ?

Just because. In C++0x you will be able to use "auto" that way.

For now, I suggest you simply use typedef.


The compiler is able to check if the type on the RHS is decided on
compile time or not, and give error if it is not.
Is it possible to implement something like this in the current C++
language?

Not really, but g++ offers the "typeof" extension (essentially the same
as C++0x "decltype").

I would really appreciate any help on this point, it is another
recurring problem in my library.

Redesign.


Cheers, & hth.,

- Alf
 
X

xtrigger303

* StephQ:
be passing a run-time decided type as template parameter. :(






Just because. In C++0x you will be able to use "auto" that way.

For now, I suggest you simply use typedef.


Not really, but g++ offers the "typeof" extension (essentially the same
as C++0x "decltype").


Redesign.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Hi to all,
check the following, it's a LITTLE bit convoluted but maybe...
Tell me what you think.
Bye,
Francesco

#include <iostream>
#include <typeinfo>

class CImpossibleToWrite1 {};

class CImpossibleToWrite2 {};

class CImpossibleToWrite3 {};

char ( & Deduct( CImpossibleToWrite1 const & ) ) [ 1 ];
char ( & Deduct( CImpossibleToWrite2 const & ) ) [ 2 ];
char ( & Deduct( CImpossibleToWrite3 const & ) ) [ 3 ];

template< int K >
struct CDeduct;

template<>
struct CDeduct< 1 >
{
typedef CImpossibleToWrite1 tType;
};

template<>
struct CDeduct< 2 >
{
typedef CImpossibleToWrite2 tType;
};

template<>
struct CDeduct< 3 >
{
typedef CImpossibleToWrite3 tType;
};

template< typename T1, typename T2 >
class CWhatever
{};

#define DEFINE_VAR( NewVarName, DeductFromObject1,
DeductFromObject2 ) \
CWhatever< CDeduct< sizeof( Deduct( DeductFromObject1 ) ) >::tType, \
CDeduct< sizeof( Deduct( DeductFromObject2 ) ) >::tType > NewVarName

int main()
{
CImpossibleToWrite1 obj1;
CImpossibleToWrite2 obj2;
CImpossibleToWrite3 obj3;

DEFINE_VAR( obj4, obj1, obj2 );
DEFINE_VAR( obj5, obj2, obj3 );

std::cout << typeid( obj4 ).name() << std::endl;
std::cout << typeid( obj5 ).name() << std::endl;
std::cin.get();
}
 
S

StephQ

* StephQ:
be passing a run-time decided type as template parameter. :(






Just because. In C++0x you will be able to use "auto" that way.

For now, I suggest you simply use typedef.

Perfect. I did some research and it seems exactly what I am searching
for.
Not really, but g++ offers the "typeof" extension (essentially the same
as C++0x "decltype").


Redesign.

Not really an option. I'm using decoration to create flexible
modifications of random number distribution samplers (to achieve
variance reduction in monte carlo simulations). Decoration is really
the natural thing to do here. And I would like to avoid function
pointers (samplers really have to be efficients). For the moment I'm
using a mix of inheritance and typedefs (because anyway only in main()
I have this problem, even if it is a common problem of the library).

Thanks

StephQ
 
S

StephQ

Hi to all,
check the following, it's a LITTLE bit convoluted but maybe...
Tell me what you think.
Bye,
Francesco

Yes, definetly convoluted :)
Thank you very much for your help!

I also discovered a boost macro for auto and typeof support.
It is not that immediate to use but I can live with it until c++0x.

Do you know if your approach has nothing in common with it?

StephQ
 
X

xtrigger303

Yes, definetly convoluted :)
Thank you very much for your help!

I also discovered a boost macro for auto and typeof support.
It is not that immediate to use but I can live with it until c++0x.

Do you know if your approach has nothing in common with it?

StephQ

I don't know how the boost typeof works (actually I have boost version
1.33.1 and I can't find it, I see it in the online documentation
though..).
Anyway I've read about mapping types to ints to simulate typeof a
while back, so probably it's implemtented in a similar way.
It's useless to reinvent the wheel, if the boost guys have done a
library it must be the BEST solution... ;-)
If you're curious try googling something like "simulate typeof".
Good luck!
Francesco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top