the code in the if-else is equivalent to
if(ARG::metrn==metrnSMD)
{ EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain); }
else
{ EventStream<Lambda> evstrTrain(fileTrain, evlsTrain); }
so the variable is not visible outside the scope of theif or else blocks.
one commonly used work around is to have a polymorphic base class. this works if the method signatures are identical for all the template specializations. eg.
#include <iostream>
struct event_stream_base
{
virtual ~event_stream_base() {} // this is the destructor;
// there is a problem with the daniweb code formatter
virtual void set_random_order(bool) = 0 ;
} ;
template< typename T > struct event_stream : event_stream_base
{
explicit event_stream( const T& v ) : member(v) {}
virtual void set_random_order(bool)
{ std::cout << member << '\n' ; }
T member ;
// ...
};
int main()
{
int a = 8 ;
event_stream_base* pesb = 0 ;
if( a > 5 ) pesb = new event_stream<double>(2.34) ;
else pesb = new event_stream<int>(73) ;
pesb->set_random_order(true) ;
delete pesb ;
}
(another is to use a meta-function; but it may be prudent to ignore this option for now.)
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
I think the original problem (causing the compiler error) is this:
EventStream<complex<Lambda<strong>>></strong> evstrTrain( ...
C++ compilers can't distinguish this from the right-shift operator. You need to have a space between them: EventStream<complex<Lambda<strong>> ></strong> evstrTrain( ...
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
> I think the original problem (causing the compiler error) is this:
> EventStream> evstrTrain( ...
he is using Visual Studio C++ 2005. which accepts this (perhaps in anticipation of c++0x).
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
well, for the simple example you posted, you could write
if(ARG::metrn==metrnSMD)
{
EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain);
if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
}
else
{
EventStream<Lambda> evstrTrain(fileTrain, evlsTrain);
if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
}
but this may not be pragmatic in more complex cases (where the second if is not proximate to the first if-else)
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
> what I intend is so simple, if (ARG::metrn) has some value declare EventStream evntstrTrain
> calling with the complex template, else declare it with the Lambda template.
if there are only a limited number of possibilities (like two in the example), you could use a variable of type boost::any http://www.boost.org/doc/html/any.html to hold the variable and do a type check later to figure out what it is.
#include <iostream>
#include <boost/any.hpp>
#include <typeinfo>
template< typename T > struct event_stream
{
explicit event_stream( const T& v ) : member(v) {}
void set_random_order(bool) { std::cout << member << '\n' ; }
T member ;
// ...
};
int main()
{
int a = 8 ;
boost::any any ;
if( a > 5 ) any = event_stream<double>(2.34) ;
else any = event_stream<int>(73) ;
// ...
if( any.type() == typeid( event_stream<double> ) )
boost::any_cast< event_stream<double> >(
any).set_random_order(true) ;
else if( any.type() == typeid( event_stream<int> ) )
boost::any_cast< event_stream<int> >(
any).set_random_order(true) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Nice! Boost rocks!
Sorry I wasn't paying attention when I responded earlier...
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229