Connecting Tech Pros Worldwide Help | Site Map

How to instantiate template when it is arguments satisfy some condition?

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#1: Nov 6 '05
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;
}

Alf P. Steinbach
Guest
 
Posts: n/a
#2: Nov 6 '05

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?
Tao Wang
Guest
 
Posts: n/a
#3: Nov 7 '05

re: How to instantiate template when it is arguments satisfy some condition?


-----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-----
PengYu.UT@gmail.com
Guest
 
Posts: n/a
#4: Nov 7 '05

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;
};

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#5: Nov 7 '05

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

Thomas Tutone
Guest
 
Posts: n/a
#6: Nov 7 '05

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

John Carson
Guest
 
Posts: n/a
#7: Nov 7 '05

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
Closed Thread