473,322 Members | 1,307 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,322 software developers and data experts.

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 1300
On May 27, 6:11 am, "PengYu...@gmail.com" <PengYu...@gmail.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...@gmail.com" <PengYu...@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);

}
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
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: ...
4
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() {}...
2
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
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...
2
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...
10
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. ...
14
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
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...
2
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.