-
February 12th, 2009, 01:37 AM
#1
[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.
Thanks for your help.
-
February 12th, 2009, 04:30 AM
#2
Re: Equivalance Typeof Operator Suggestion
Partial specialisation?
Code:
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
-
February 12th, 2009, 05:41 AM
#3
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 );
}
};
Last edited by GNiewerth; February 12th, 2009 at 05:43 AM.
- Guido
-
February 12th, 2009, 07:40 AM
#4
Re: Equivalance Typeof Operator Suggestion
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.
-
February 13th, 2009, 03:49 AM
#5
Re: Equivalance Typeof Operator Suggestion
GNiewerth, wonder approach. I will try it out when i come back from university.
A billion thanks to you.
Thanks for your help.
-
February 15th, 2009, 04:08 AM
#6
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.
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|