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.
struct arcType
{
};
template <typename T, class edgeType>
class graph
{
};
template <typename T>
class graph<T, arcType>
{
};
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
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 );
}
};
Last edited by GNiewerth; February 12th, 2009 at 04:43 AM.
Boost has a typeof macro. gcc supports it natively and its emulated elsewhere. Its here.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Bookmarks