[RESOLVED] Equivalance Typeof Operator Suggestion
Hello to all expert, i have a class template like this.
Code:
template <typename T, class edgeType>
class graph;
I would like to implement MF differently when different type of edgeType is supplied, for instance, arcs was supplied, then i identify this as directed graph and i just insert one set of edge.
If edge, then i insert two sets of edge.
Any typeof operator in C++ STL ?
Thanks.
Re: Equivalance Typeof Operator Suggestion
Partial specialisation?
Code:
struct arcType
{
};
template <typename T, class edgeType>
class graph
{
};
template <typename T>
class graph<T, arcType>
{
};
Re: Equivalance Typeof Operator Suggestion
There´s a typeid operator that relies on RTTI and provides type information at runtime. Providing specialized templates allows compile time decision, and that´s probably what you want.
You can´t specialize member templates and you can´t partially specialize non member function, but you can use a (partially) specialized inserter template.
Code:
// generic Edge Inserter
template<typename Container, typename EdgeType>
struct EdgeInserter
{
void operator()( Container& c, const EdgeType& e )
{
// insert edge into container
c.push_back( e );
}
};
// specialized Edge Inserter
template<typename Container>
struct EdgeInserter<Container,Arc>
{
void operator()( Container&c, const Arc& a )
{
// insert arc into container
c.push_back( a );
}
};
template<typename T, typename EdgeType>
struct graph
{
EdgeInserter<EdgeContainerType,EdgeType> Inserter;
// Container to hold edges (whatever this is)
EdgeContainerType Edges;
void add_edge( const EdgeType& NewEdge )
{
// use inserter to add edge
Inserter( Edges, NewEdge );
}
};
Re: Equivalance Typeof Operator Suggestion
Boost has a typeof macro. gcc supports it natively and its emulated elsewhere. Its here.
Re: Equivalance Typeof Operator Suggestion
GNiewerth, wonder approach. I will try it out when i come back from university.
A billion thanks to you.
Re: Equivalance Typeof Operator Suggestion
I think a bit shorter if you use functor CT polymorphism.
My approach will be a Non MF that called appropriate MF.
Question 1:
What is the pro and cons using functor CT polymorphism ?
The thinking logic is from based on the edge type then coded different implement strategy.
I never think this out. Now i know.
A billion thanks for your help.