473,725 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I use typedef to define types used in the return type in template function?

I have the following sample program, which can convert function object
with 1 argument into function object with 2 arguments. It can also do +
between function object of the same type.

The last line is very long. I'm wondering if there is any way to
suppress it. I can only think of typedef. But I'm not sure whether I
can use typedef for the return type.

Would you please help me? Please don't be daunted by the length of the
code.

Thanks,
Peng

/*main.cc*/
#include "expression_tem plates.h"
#include <cassert>
#include <complex>

int main(void) {
sample_1_arg_fu n<std::complex< double> > f;
std::cout << cast1stArg(f)(0 , 1) << std::endl;
std::cout << cast2ndArg(f)(0 , 1) << std::endl;
std::cout << (cast2ndArg(f) + cast2ndArg(f))( 2, 1) << std::endl;
}

/*expression_tem plates.h*/
#include <iostream>
#include <functional>
#include <algorithm>
#include <limits>

template <typename T>
class sample_1_arg_fu n {
public:
typedef T return_type;
return_type operator()(int i) const { return (i>= the_min_limit()
&& i <= the_max_limit() )?i:0; }
int the_min_limit() const { return std::numeric_li mits<int>::min( );
}
int the_max_limit() const { return std::numeric_li mits<int>::max( );
}
};

struct LimitsUnion {
int min_limit(int min_limit1, int min_limit2) const { return
std::min(min_li mit1, min_limit2); }
int max_limit(int max_limit1, int max_limit2) const { return
std::max(max_li mit1, max_limit2); }
};

struct LimitsIntersect ion {
int min_limit(int min_limit1, int min_limit2) const { return
std::max(min_li mit1, min_limit2); }
int max_limit(int max_limit1, int max_limit2) const { return
std::min(max_li mit1, max_limit2); }
};

template <typename BinOp>
struct BinOpTraits;

template <typename T>
struct BinOpTraits<std ::plus<T> >{
typedef LimitsUnion limit_op;
};

template <typename T1, typename ExprT1, typename T2, typename ExprT2,
typename BinOp>
class Expr2Args;

template <typename T, typename ExprT>
class Expr2Args<T, ExprT, void, void, void> {
public:
typedef T return_type;
Expr2Args(const ExprT e) : _e(e) {}
return_type operator()(int i1, int) const { return _e(i1); }
int the_1st_min_lim it() const { return e.the_min_limit (); }
int the_1st_max_lim it() const { return e.the_max_limit (); }
int the_2nd_min_lim it() const { return
std::numeric_li mits<int>::min( ); }
int the_2nd_max_lim it() const { return
std::numeric_li mits<int>::max( ); }
private:
const ExprT _e;
};

template <typename T, typename ExprT>
class Expr2Args<void, void, T, ExprT, void> {
public:
typedef T return_type;
Expr2Args(const ExprT e) : _e(e) {}
return_type operator()(int, int i2) const { return _e(i2); }
int the_1st_min_lim it() const { return
std::numeric_li mits<int>::min( ); }
int the_1st_max_lim it() const { return
std::numeric_li mits<int>::max( ); }
int the_2nd_min_lim it() const { return e.the_min_limit (); }
int the_2nd_max_lim it() const { return e.the_max_limit (); }
private:
const ExprT _e;
};

template <typename T, typename ExprT1, typename ExprT2, typename BinOp>
class Expr2Args<T, ExprT1, T, ExprT2, BinOp> {
typedef typename BinOpTraits<Bin Op>::limit_op limit_op;
public:
typedef T return_type;
Expr2Args(const ExprT1 e1, const ExprT2 e2) : _e1(e1), _e2(e2) {}
return_type operator()(int i1, int i2) const { return
BinOp()(_e1(i1, i2), _e2(i1, i2)); }
int the_1st_min_lim it() const { return
limit_op::min_l imit(_e1.the_1s t_min_limit(), _e2.the_1st_min _limit());
}
int the_1st_max_lim it() const { return
limit_op::max_l imit(_e1.the_1s t_max_limit(), _e2.the_1st_max _limit());
}
int the_2nd_min_lim it() const { return
limit_op::min_l imit(_e1.the_2n d_min_limit(), _e2.the_2nd_min _limit());
}
int the_2nd_max_lim it() const { return
limit_op::max_l imit(_e1.the_2n d_max_limit(), _e2.the_2nd_max _limit());
}
private:
const ExprT1 _e1;
const ExprT2 _e2;
};

template <typename ExprT>
Expr2Args<typen ame ExprT::return_t ype, ExprT, void, void, void>
cast1stArg(cons t ExprT e) {
return Expr2Args<typen ame ExprT::return_t ype, ExprT, void, void,
void>(e);
}

template <typename ExprT>
Expr2Args<void, void, typename ExprT::return_t ype, ExprT, void>
cast2ndArg(cons t ExprT e) {
return Expr2Args<void, void, typename ExprT::return_t ype, ExprT,
void>(e);
}

template <typename T11, typename ExprT11, typename T12, typename
ExprT12, typename BinOp1, typename T21, typename ExprT21, typename T22,
typename ExprT22, typename BinOp2>
Expr2Args<typen ame Expr2Args<T11, ExprT11, T12, ExprT12,
BinOp1>::return _type, Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>,
typename Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>::return _type,
Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>, std::plus<typen ame
Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>::return _type> >
operator+(const Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>& e1,
const Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>& e2) {
return Expr2Args<typen ame Expr2Args<T11, ExprT11, T12, ExprT12,
BinOp1>::return _type, Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>,
typename Expr2Args<T21, ExprT21, T22, ExprT22, BinOp1>::return _type,
Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>, std::plus<typen ame
Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>::return _type> >(e1, e2);
}

Nov 7 '05 #1
2 2345

Pe*******@gmail .com wrote:
I have the following sample program, which can convert function object
with 1 argument into function object with 2 arguments. It can also do +
between function object of the same type.

what is your main goal?
The last line is very long. I'm wondering if there is any way to
suppress it. I can only think of typedef. But I'm not sure whether I
can use typedef for the return type.

Would you please help me? Please don't be daunted by the length of the
code.

Thanks,
Peng

/*main.cc*/
#include "expression_tem plates.h"
#include <cassert>
#include <complex>

int main(void) {
sample_1_arg_fu n<std::complex< double> > f;
std::cout << cast1stArg(f)(0 , 1) << std::endl;
std::cout << cast2ndArg(f)(0 , 1) << std::endl;
std::cout << (cast2ndArg(f) + cast2ndArg(f))( 2, 1) << std::endl;
}

/*expression_tem plates.h*/
#include <iostream>
#include <functional>
#include <algorithm>
#include <limits>

template <typename T>
class sample_1_arg_fu n {
public:
typedef T return_type;
return_type operator()(int i) const { return (i>= the_min_limit()
&& i <= the_max_limit() )?i:0; }
int the_min_limit() const { return std::numeric_li mits<int>::min( );
}
int the_max_limit() const { return std::numeric_li mits<int>::max( );
}
};

struct LimitsUnion {
int min_limit(int min_limit1, int min_limit2) const { return
std::min(min_li mit1, min_limit2); }
int max_limit(int max_limit1, int max_limit2) const { return
std::max(max_li mit1, max_limit2); }
};

any reasons not to use "static max_limit(int, int)"?

template <typename T11, typename ExprT11, typename T12, typename
ExprT12, typename BinOp1, typename T21, typename ExprT21, typename T22,
typename ExprT22, typename BinOp2>
Expr2Args<typen ame Expr2Args<T11, ExprT11, T12, ExprT12,
BinOp1>::return _type, Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>,
typename Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>::return _type,
Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>, std::plus<typen ame
Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>::return _type> >
operator+(const Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>& e1,
const Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>& e2) {
return Expr2Args<typen ame Expr2Args<T11, ExprT11, T12, ExprT12,
BinOp1>::return _type, Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>,
typename Expr2Args<T21, ExprT21, T22, ExprT22, BinOp1>::return _type,
Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>, std::plus<typen ame
Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>::return _type> >(e1, e2);
}


may be this:

template <typename T1, typename T2>
Expr2Args< typename T1::return_type , T1, typename T2::return_type ,
T2, std::plus<typen ame T1::return_type > >
operator+(const T1& e1, const T2& e2) {
return Expr2Args< typename T1::return_type , T1, typename
T2::return_type ,T2, std::plus<typen ame T1::return_type > >(e1, e2);
}

Nov 7 '05 #2

Aleksey Loginov wrote:

may be this:

template <typename T1, typename T2>
Expr2Args< typename T1::return_type , T1, typename T2::return_type ,
T2, std::plus<typen ame T1::return_type > >
operator+(const T1& e1, const T2& e2) {
return Expr2Args< typename T1::return_type , T1, typename
T2::return_type ,T2, std::plus<typen ame T1::return_type > >(e1, e2);
}


my mistake.

template< typename T11, typename ExprT11, typename T12, typename
ExprT12, typename BinOp1, typename T21, typename ExprT21, typename T22,

typename ExprT22, typename BinOp2, typename T1=Expr2Args<T1 1, ExprT11,
T12, ExprT12, BinOp1>, typename T2=Expr2Args<T2 1, ExprT21, T22,
ExprT22, BinOp2> >
Expr2Args< typename T1::return_type , T1, typename T2::return_type ,
T2, std::plus<typen ame T1::return_type > >
operator+(const Expr2Args<T11, ExprT11, T12, ExprT12, BinOp1>& e1,
const Expr2Args<T21, ExprT21, T22, ExprT22, BinOp2>& e2) { ... }

and this
http://osl.iu.edu/~tveldhui/papers/t...ues01.html#l14

Nov 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7109
by: Arkadiy Vertleyb | last post by:
Hi all, I am having a problem trying to overload a function template, based on a typedef, such as: template<class T> struct A {}; template<class T>
6
1908
by: Dave | last post by:
Hello all, I have included below a header file for a module I'm working on. Please note the line marked "***** ERROR HERE *****". At this line, the type dl_t is not being seen for some reason. Can anybody see why this is? Thanks, Dave
2
3636
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
2
1644
by: marco | last post by:
the problem: I use a typedef inside a class template, than I use this type (dim_v<N1>::Type) to define the argument of a template function f but when I call this function from main, the compiler (gcc 3.4.1) tell me: "no matching function found". If someone, more expert than me, could tell me what is wrong I would be very happy
12
15637
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
12
2323
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include <iostream> using namespace std; template <class ClassB> class ClassA
7
1453
by: Michal Nazarewicz | last post by:
Let say I have a class template Set which represent set of objects of given type, and a struct template Pair representing an ordered pair, ie: #v+ template<class T> class Set { /* ... whatever ... */ public: bool exists(const T &element) { /* ... */ }
12
4647
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))();
3
1851
by: Adam Nielsen | last post by:
Hi everyone, Yet another syntax problem that's baffling me with templates. I want to instantiate a template with a single parameter as per normal, however the parameter is actually a template class itself, with all *its* parameters filled out (in the form of a typedef.) I can't work out how to break apart the typedef to reveal what data types were used to create it in the first place. Here is some example code that demonstrates the...
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.