By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,920 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,920 IT Pros & Developers. It's quick & easy.

compile time function selection

P: n/a
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
Jul 27 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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);
}

Jul 27 '05 #2

P: n/a
>
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.

Jul 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.