| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Abhishek Saksena" |
| Date: |
27 Jul 2005 02:06:13 AM |
| Object: |
compile time function selection |
Is it possible using Boost mpl library:-
Assume any class implementing a function "connect" with two arugments of
fixed types
class protocol1
{
connect(T0 & t0, T1 &t1 ){..} //fixed types T0 and T1
};
class protocol2
{
connect(T2 & t0, T3 &t1 ){..} // Fixed types T2 and T3
};
Now a function say "connect_protocols" which take two protocols of above
mentioned protocol types
template<typename P0, typename P1>
connect_protocols(P0& p0, P1 & p1)
{..}
What is requires in the body of "connect_protocols" function is a checking
meachimism to use correct connect function avilable from one of the class
(protocol1 or protocol2) without genrating the compile time error.
However if no appropriate connect function is avilable (after checking both
the protocol classes) then genrate compile time error.
Is it possible?
Abhsishek
.
|
|
| User: "" |
|
| Title: Re: compile time function selection |
27 Jul 2005 03:24:37 AM |
|
|
What is requires in the body of "connect_protocols" function is a checking
meachimism to use correct connect function avilable from one of the class
(protocol1 or protocol2) without genrating the compile time error.
However if no appropriate connect function is avilable (after checking both
the protocol classes) then genrate compile time error.
Is it possible?
Have you tried template specialisation?
I'm not sure if I read correctly what you asked but the following might
be a solution to your problem.
template<class T, class U>
void connect_protocols(T &First, T &Second);
template<>
void connect_protocols(P0 &First, P1 &Second)
{
// perform connect here
}
template<>
void connect_protocols(P2 &First, P3 &Second)
{
// perform connect here
}
This should result in an compile time error unless you fill in the
correct protocols.
.
|
|
|
|
| User: "Maxim Yegorushkin" |
|
| Title: Re: compile time function selection |
27 Jul 2005 03:22:07 AM |
|
|
Abhishek Saksena wrote:
Is it possible using Boost mpl library:-
It's not needed here.
Assume any class implementing a function "connect" with two arugments of
fixed types
class protocol1
{
connect(T0 & t0, T1 &t1 ){..} //fixed types T0 and T1
};
class protocol2
{
connect(T2 & t0, T3 &t1 ){..} // Fixed types T2 and T3
};
Now a function say "connect_protocols" which take two protocols of above
mentioned protocol types
template<typename P0, typename P1>
connect_protocols(P0& p0, P1 & p1)
{..}
You forgot to state the return type of the function.
What is requires in the body of "connect_protocols" function is a checking
meachimism to use correct connect function avilable from one of the class
(protocol1 or protocol2) without genrating the compile time error.
However if no appropriate connect function is avilable (after checking both
the protocol classes) then genrate compile time error.
Is it possible?
It is. Just add factory functions that take the same arguments as your
protocol classes's constructors do:
protocol1 create(T0 & t0, T1 &t1) { return protocol1(t0, t1); }
// the same for protocol2
template<typename P0, typename P1>
void connect_protocols(P0& p0, P1 & p1)
{
create(p0, p1);
}
.
|
|
|
|

|
Related Articles |
|
|