473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Nov 6 '05 #1
6 1632
* 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
-----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

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

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

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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
9
by: jc | last post by:
Hi all, I have a data type to use that I can't modify its codes. E.g. This template data type is data_type. When I declare a variable of this data_type, I write as following: data_type(8,...
3
by: =?iso-8859-1?B?Tm9yZGz2dw==?= | last post by:
Hey there, C++ Coders! I am learning multi-threading with boost and have come up with the following code example shown below. This example implements a test of the producer-consumer design...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
3
by: nooneinparticular314159 | last post by:
I'm trying to do a template with specialization. I declare two templates, one of which looks like: template<int a, int b> struct MOO { enum{ VALUE = b == 0 ? a: MOO<some stuff>::VALUE };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.