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

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.