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

P

PengYu.UT

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<double> a;
double x = 0;
a.doit(x);
}
 
K

kingfox

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<double> a;
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.
 
P

pawel.kunio

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<double> a;
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.
 
M

Markus Schoder

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<double> a;
double x = 0;
a.doit(x);
}

Define a template function

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

and call it in A::doit().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top