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

P

PengYu.UT

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

Alf P. Steinbach

* (e-mail address removed):
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?

template< typename A, typename B >
struct IsSameType{ enum{ yes=false }; };

template< typename T >
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;
BOOST_STATIC_ASSERT((

};

int main(int argc, char *argv[]){
Expr2<Expr1<int>, Expr1<double> > a;
}
 
T

Tao Wang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

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

iD8DBQFDbpjlRS5AkKgtcCcRAhsEAJ0TZEl0k6Z9U2mAOV2CzIo2MoNOQgCaA4kt
Fg7JcU7n5qYwrRZOC734hc4=
=agtk
-----END PGP SIGNATURE-----
 
P

PengYu.UT

Tao said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

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

PengYu.UT

Alf said:
* (e-mail address removed):
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?

template< typename A, typename B >
struct IsSameType{ enum{ yes=false }; };

template< typename T >
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;
BOOST_STATIC_ASSERT((

};

int main(int argc, char *argv[]){
Expr2<Expr1<int>, Expr1<double> > a;
}
Do you know how BOOST_STATIC_ASSERT is implemented? I might not be able
to use boost.

Thanks,
Peng
 
J

John Carson

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top