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

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

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

Nov 6 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* Pe*******@gmail.com:
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 >
struct IsSameType<T, T>{ enum{ yes=true }; };

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((
IsSameType<typename E1::return_type, typename E2::return_type>::yes
));
};

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


--
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?
Nov 6 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pe*******@gmail.com wrote:
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

iD8DBQFDbpjlRS5AkKgtcCcRAhsEAJ0TZEl0k6Z9U2mAOV2CzI o2MoNOQgCaA4kt
Fg7JcU7n5qYwrRZOC734hc4=
=agtk
-----END PGP SIGNATURE-----
Nov 7 '05 #3

P: n/a

Tao Wang wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pe*******@gmail.com wrote:
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;
};

Nov 7 '05 #4

P: n/a

Alf P. Steinbach wrote:
* Pe*******@gmail.com:
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 >
struct IsSameType<T, T>{ enum{ yes=true }; };

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((
IsSameType<typename E1::return_type, typename E2::return_type>::yes
));
};

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

Nov 7 '05 #5

P: n/a

Pe*******@gmail.com wrote:
Do you know how BOOST_STATIC_ASSERT is implemented?


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

Nov 7 '05 #6

P: n/a
<Pe*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
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;
}
--
John Carson
Nov 7 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.