How to instantiate template when it is arguments satisfy some condition? 
November 6th, 2005, 10:35 PM
| | | How to instantiate template when it is arguments satisfy some condition?
For example, I want the return_type of the two arguments of Expr2 be
the same. Otherwise, the compilor should give an error.
Would you please tell me how to do that?
Thanks,
Peng
template <typename T>
struct Expr1{
typedef T return_type;
};
template <typename E, typename T>
struct Expr2;
template <typename E1, typename E2>
struct Expr2 {//I want E1 and E2's return_type be the same.
typedef typename E1::return_type return_type;
};
int main(int argc, char *argv[]){
Expr2<Expr1<int>, Expr1<double> > a;
} | 
November 6th, 2005, 10:55 PM
| | | Re: How to instantiate template when it is arguments satisfy some condition?
* PengYu.UT@gmail.com:[color=blue]
> For example, I want the return_type of the two arguments of Expr2 be
> the same. Otherwise, the compilor should give an error.
>
> Would you please tell me how to do that?[/color]
template< typename A, typename B >
struct IsSameType{ enum{ yes=false }; };
template< typename T >
struct IsSameType<T, T>{ enum{ yes=true }; };
[color=blue]
> template <typename T>
> struct Expr1{
> typedef T return_type;
> };
>
> template <typename E, typename T>
> struct Expr2;
>
> template <typename E1, typename E2>
> struct Expr2 {//I want E1 and E2's return_type be the same.
> typedef typename E1::return_type return_type;[/color]
BOOST_STATIC_ASSERT((
IsSameType<typename E1::return_type, typename E2::return_type>::yes
));
[color=blue]
> };
>
> int main(int argc, char *argv[]){
> Expr2<Expr1<int>, Expr1<double> > a;
> }[/color]
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | 
November 6th, 2005, 11:05 PM
| | | Re: How to instantiate template when it is arguments satisfy somecondition?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 PengYu.UT@gmail.com wrote:[color=blue]
> For example, I want the return_type of the two arguments of Expr2 be
> the same. Otherwise, the compilor should give an error.
>
> Would you please tell me how to do that?
>
> Thanks,
> Peng
>
>
> template <typename T>
> struct Expr1{
> typedef T return_type;
> };
>
> template <typename E, typename T>
> struct Expr2;
>
> template <typename E1, typename E2>
> struct Expr2 {//I want E1 and E2's return_type be the same.
> typedef typename E1::return_type return_type;
> };
>
> int main(int argc, char *argv[]){
> Expr2<Expr1<int>, Expr1<double> > a;
> }
>[/color]
To avoid the problem, why don't use one type, if two type are actually same?
template <typename T>
struct Expr1{
typedef T return_type;
};
template <typename E>
struct Expr2 {
typedef typename E::return_type return_type;
};
int main(int argc, char *argv[]){
Expr2<Expr1<int> > a;
return 0;
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFDbpjlRS5AkKgtcCcRAhsEAJ0TZEl0k6Z9U2mAOV2CzI o2MoNOQgCaA4kt
Fg7JcU7n5qYwrRZOC734hc4=
=agtk
-----END PGP SIGNATURE----- | 
November 6th, 2005, 11:35 PM
| | | Re: How to instantiate template when it is arguments satisfy some condition?
Tao Wang wrote:[color=blue]
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> PengYu.UT@gmail.com wrote:[color=green]
> > For example, I want the return_type of the two arguments of Expr2 be
> > the same. Otherwise, the compilor should give an error.
> >
> > Would you please tell me how to do that?
> >
> > Thanks,
> > Peng
> >
> >
> > template <typename T>
> > struct Expr1{
> > typedef T return_type;
> > };
> >
> > template <typename E, typename T>
> > struct Expr2;
> >
> > template <typename E1, typename E2>
> > struct Expr2 {//I want E1 and E2's return_type be the same.
> > typedef typename E1::return_type return_type;
> > };
> >
> > int main(int argc, char *argv[]){
> > Expr2<Expr1<int>, Expr1<double> > a;
> > }
> >[/color]
>
> To avoid the problem, why don't use one type, if two type are actually same?
>
> template <typename T>
> struct Expr1{
> typedef T return_type;
> };
>
> template <typename E>
> struct Expr2 {
> typedef typename E::return_type return_type;
> };
>
> int main(int argc, char *argv[]){
> Expr2<Expr1<int> > a;
> return 0;
> }[/color]
Because I might have Expr3 which can be used as the template arguments
of Expr2.
template <typename T>
struct Expr3{
typedef std::complex<T> return_type;
}; | 
November 6th, 2005, 11:45 PM
| | | Re: How to instantiate template when it is arguments satisfy some condition?
Alf P. Steinbach wrote:[color=blue]
> * PengYu.UT@gmail.com:[color=green]
> > For example, I want the return_type of the two arguments of Expr2 be
> > the same. Otherwise, the compilor should give an error.
> >
> > Would you please tell me how to do that?[/color]
>
> template< typename A, typename B >
> struct IsSameType{ enum{ yes=false }; };
>
> template< typename T >
> struct IsSameType<T, T>{ enum{ yes=true }; };
>
>[color=green]
> > template <typename T>
> > struct Expr1{
> > typedef T return_type;
> > };
> >
> > template <typename E, typename T>
> > struct Expr2;
> >
> > template <typename E1, typename E2>
> > struct Expr2 {//I want E1 and E2's return_type be the same.
> > typedef typename E1::return_type return_type;[/color]
>
> BOOST_STATIC_ASSERT((
> IsSameType<typename E1::return_type, typename E2::return_type>::yes
> ));
>[color=green]
> > };
> >
> > int main(int argc, char *argv[]){
> > Expr2<Expr1<int>, Expr1<double> > a;
> > }[/color][/color]
Do you know how BOOST_STATIC_ASSERT is implemented? I might not be able
to use boost.
Thanks,
Peng | 
November 7th, 2005, 12:05 AM
| | | Re: How to instantiate template when it is arguments satisfy some condition? PengYu.UT@gmail.com wrote:
[color=blue]
> Do you know how BOOST_STATIC_ASSERT is implemented?[/color]
Yes.
Why don't you look at the source code for yourself? You can download
it and use it, subject to Boost's license. http://www.boost.org/boost/static_assert.hpp
You can also find a similar static assert macro in Modern C++ Design.
Best regards,
Tom | 
November 7th, 2005, 12:05 AM
| | | Re: How to instantiate template when it is arguments satisfy some condition?
<PengYu.UT@gmail.com> wrote in message
news:1131319252.336150.257160@f14g2000cwb.googlegr oups.com[color=blue]
> For example, I want the return_type of the two arguments of Expr2 be
> the same. Otherwise, the compilor should give an error.
>
> Would you please tell me how to do that?
>
> Thanks,
> Peng
>
>
> template <typename T>
> struct Expr1{
> typedef T return_type;
> };
>
> template <typename E, typename T>
> struct Expr2;
>
> template <typename E1, typename E2>
> struct Expr2 {//I want E1 and E2's return_type be the same.
> typedef typename E1::return_type return_type;
> };
>
> int main(int argc, char *argv[]){
> Expr2<Expr1<int>, Expr1<double> > a;
> }[/color]
template <typename T>
struct Expr1
{
typedef T return_type;
};
// forward declare only
template<class A, class B>
class EqualTypes;
// specialize for A == B
template<class A>
class EqualTypes<A,A>
{};
template <typename E1, typename E2>
struct Expr2
{
EqualTypes<typename E1::return_type, typename E2::return_type> eq;
};
int main()
{
Expr2<Expr1<int>, Expr1<double> > a;
}
--
John Carson | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|