473,770 Members | 6,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make function body different depending on the template type? (MPL)

Hi,

In the function body of doit, I want to assign -x to _a if x is a real
type. I want to assign conj(x) to _a if it is a complex type.

I could define
template <typename T>
class A<std::complex< T {
.....
};

But there are some redundancies between the definition class
A<std::complex< T and class A<T>.

I'm wondering if boost MPL can be without define a new class or the
class with a different template parameters.

Thanks,
Peng
template <typename T>
class A {
public:
A() {}
void doit(const T &x) {
....
}
private:
T _a;
};

int main() {
A<doublea;
double x = 0;
a.doit(x);
}

May 26 '07 #1
3 1332
On May 27, 6:11 am, "PengYu...@gmai l.com" <PengYu...@gmai l.comwrote:
Hi,

In the function body of doit, I want to assign -x to _a if x is a real
type. I want to assign conj(x) to _a if it is a complex type.

I could define
template <typename T>
class A<std::complex< T {
....

};

But there are some redundancies between the definition class
A<std::complex< T and class A<T>.

I'm wondering if boost MPL can be without define a new class or the
class with a different template parameters.

Thanks,
Peng

template <typename T>
class A {
public:
A() {}
void doit(const T &x) {
....
}
private:
T _a;

};

int main() {
A<doublea;
double x = 0;
a.doit(x);

}- Hide quoted text -

- Show quoted text -
The only way I know is to overload doit() method. I'm afraid you have
to write doit(...) method for every data type.

May 27 '07 #2
On May 27, 12:11 am, "PengYu...@gmai l.com" <PengYu...@gmai l.com>
wrote:
Hi,

In the function body of doit, I want to assign -x to _a if x is a real
type. I want to assign conj(x) to _a if it is a complex type.

I could define
template <typename T>
class A<std::complex< T {
....

};

But there are some redundancies between the definition class
A<std::complex< T and class A<T>.

I'm wondering if boost MPL can be without define a new class or the
class with a different template parameters.

Thanks,
Peng

template <typename T>
class A {
public:
A() {}
void doit(const T &x) {
....
}
private:
T _a;

};

int main() {
A<doublea;
double x = 0;
a.doit(x);

}
There is possibility to declare template to be independent of type,
but to add the partial specializations in implementation to define
that implementation for complex and real types will vary.

I will prepare the example basing on your program and will upload it
shortly.

May 27 '07 #3
On Sat, 26 May 2007 15:11:50 -0700, Pe*******@gmail .com wrote:
Hi,

In the function body of doit, I want to assign -x to _a if x is a real
type. I want to assign conj(x) to _a if it is a complex type.

I could define
template <typename T>
class A<std::complex< T {
....
};

But there are some redundancies between the definition class
A<std::complex< T and class A<T>.

I'm wondering if boost MPL can be without define a new class or the
class with a different template parameters.

Thanks,
Peng
template <typename T>
class A {
public:
A() {}
void doit(const T &x) {
....
}
private:
T _a;
};

int main() {
A<doublea;
double x = 0;
a.doit(x);
}
Define a template function

template<class Tvoid f(const T &x, T &a)
{
a = -x;
}
template<class Tvoid f(const std::complex<T& x, std::complex<T& a)
{
a = conj(x);
}

and call it in A::doit().

--
Markus Schoder
May 28 '07 #4

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

Similar topics

5
3205
by: ian | last post by:
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++ Design by Andrei Alexandrescu. The test code is listed below and the compiler error message that is generated is: testPolicy.cpp(25) : error C2984: 'CPolicy' : template parameters '<template parameter>' and '<template parameter>' do not match testPolicy.cpp(22) : see declaration of 'CPolicy' I am working with version 1 of Microsoft VC++ and running WinXP Pro....
4
2308
by: Marc Schellens | last post by:
I posted a similar question some time ago, but didn't get an satisfying answer. Lets say I have a template and five integer and two floating types. template <typename T> class A { A() {} ~A() {}
2
1247
by: John Harrison | last post by:
I want to write a templated constructor with a non-type template argument, like this. class X { public: X() : val(0) {} template <int I> X(X const&, X const&) : val(I) {}
9
1883
by: Peter Koch Larsen | last post by:
This question has undoubtedly come up zillions of times before, but I've not been able to google for an answer. My problem can be reproduced as follows: template<typename T, bool b> struct bad { void f(T const& t); }; // wanting to specialise for b = true
2
3965
by: Abhishek Saksena | last post by:
Is it possible using Boost mpl library:- Assume any class implementing a function "connect" with two arugments of fixed types class protocol1 { connect(T0 & t0, T1 &t1 ){..} //fixed types T0 and T1 };
10
2098
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ========================= template <class T> myClass {....} ;
14
3823
by: Joseph Turian | last post by:
Let's say I have a function template as follows: template<class T, class U> foo { ... } However, I'd like to have different behavior if T == U. How can I check that?
2
1985
by: psujkov | last post by:
Hi everybody, let us have some class A with const static int variable var with compile-time well-known value. let us have some function f(), which has return type of A. and let us have some template struct with non- type template parameter : template <int aZ. having it all there is a question : why we cannot write smth like this : Z< f()::varz ? there is no need in real call to function f - it can be not defined as all (declared only),...
2
13271
by: Kaushal | last post by:
#include <iostream> #include <boost/mpl/vector.hpp> #include <boost/mpl/find.hpp> using namespace std ; using namespace boost ; template <typename numericTypes> struct evalThis
0
9591
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
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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
10001
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
9867
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...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
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...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
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

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.