How to define the class based on the detailes of the template argument?

P

PengYu.UT

Hi,

Suppose I have the following code. I want A::show() behaves differently
for template argument with or without std::complex. But it doesn't
work. Do you know how to do that?

Best wishes,
Peng

#include <iostream>

template <typename Tp>
class A{
public:
A(){};
void show();
};

template <typename Tp>
void A<Tp>::show(){
std::cout << "no complex" << std::endl;
}

template <std::complex<Tp> >
void A<std::complex<Tp> >::show(){
std::cout << "complex" << std::endl;
}

int main(int argc, char *argv[]) {
A<double> a;
A<std::complex<double> > b;
a.show();
b.show();
}
 
R

Rolf Magnus

Hi,

Suppose I have the following code. I want A::show() behaves differently
for template argument with or without std::complex. But it doesn't
work.

Always include a definition of "doesn't work" in your postings.
Do you know how to do that?

What you are doing is a (partial) template specialization, and you cannot
specialize single member functions of class templates. You have to
specialize the whole class template.
Another problem is the syntax. See below.
#include <iostream>

template <typename Tp>
class A{
public:
A(){};
void show();
};

template <typename Tp>
void A<Tp>::show(){
std::cout << "no complex" << std::endl;
}

template <std::complex<Tp> >
void A<std::complex<Tp> >::show(){
std::cout << "complex" << std::endl;
}

That would have to be:

template <>
template <typename Tp>
class A<std::complex<Tp> >
{
public:
A(){};
void show();
};

template <>
template<typename Tp>
void A<std::complex<Tp> >::show()
{
std::cout << "complex" << std::endl;
}

At least that's what my compiler accepts. :)
int main(int argc, char *argv[]) {
A<double> a;
A<std::complex<double> > b;
a.show();
b.show();
}
 
P

PengYu.UT

I'm sorry that if I didn't say it clearly.

Based on your post, the program became the following things. But if you
look at them closely, the declaration of class A and class
A<std::complex<Tp> > are the same. Is there anyway to eliminate the
redundance? Because later, when the code is maintained it has to take
double effort to change the definition at both places, if I program
like this.

Best wishes,
Peng

#include <iostream>
#include <complex>

template <typename Tp>
class A{
public:
A(){};
void show();
};

template <>
template <typename Tp>
class A<std::complex<Tp> > //code segment is the same with class A
{
public:
A(){};
void show();
};

template <>
void A<double>::show(){
std::cout << "no complex" << std::endl;
}

template <>
template <typename Tp>
void A<std::complex<Tp> >::show(){
std::cout << "complex" << std::endl;
}

int main(int argc, char *argv[]) {
A<double> a;
A<std::complex<double> > b;
a.show();
b.show();
}
 
K

Kai-Uwe Bux

Hi,

Suppose I have the following code. I want A::show() behaves differently
for template argument with or without std::complex. But it doesn't
work. Do you know how to do that?

[snip}


The following is an often used idiom for checking those things:

#include <iostream>
#include <complex>


template < typename T >
struct check_complex {

enum { value = 0 };

};

template < typename T >
struct check_complex < std::complex< T > > {

enum { value = 1 };

};


int main ( void ) {

std::cout << check_complex< int >::value
<< " "
<< check_complex< std::complex< double > >::value
<< '\n';
}


You can easily modify it to fit your original problem on the nose.


Best

Kai-Uwe Bux
 
P

PengYu.UT

I changed the program as follows. It seems that I have to define void
A<std::complex<T> >::show(), otherwise there is some link problem. But
if I do define like that, I don't see "value" is useful. Is there any
better way to use "value" without defining "show" twice?

Thanks,
Peng

#include <iostream>
#include <complex>

template <typename T>
class A{
public:
A(){}
void show();
enum { value = 0 };
};

template <typename T>
class A<std::complex<T> >{
public:
A(){}
void show();
enum { value = 1 };
};

template <typename T>
void A<T>::show(){
if(value == 1)
std::cout << "complex" << std::endl;
else
std::cout << "no complex" << std::endl;
}

/*template <typename T>
void A<std::complex<T> >::show(){
if(value == 1)
std::cout << "complex" << std::endl;
else
std::cout << "no complex" << std::endl;
}*/

int main ( void ) {
A<double> a;
A<std::complex<double> > b;
a.show();
b.show();
}
 
R

Rolf Magnus

I changed the program as follows. It seems that I have to define void
A<std::complex<T> >::show(), otherwise there is some link problem. But
if I do define like that, I don't see "value" is useful. Is there any
better way to use "value" without defining "show" twice?

The idea was to put the enum in a separate class, not into class A itself.
 
P

PengYu.UT

I don't quite get the idea. Would please show me a short example how to
use it? Or would please point me to some webpage which discuss this?
Thanks.

BTW, is the technique that you pointed called "traits"?

Peng
 
K

Kai-Uwe Bux

I changed the program as follows. It seems that I have to define void
A<std::complex<T> >::show(), otherwise there is some link problem. But
if I do define like that, I don't see "value" is useful. Is there any
better way to use "value" without defining "show" twice?

Thanks,
Peng

#include <iostream>
#include <complex>

template <typename T>
class A{
public:
A(){}
void show();
enum { value = 0 };
};

template <typename T>
class A<std::complex<T> >{
public:
A(){}
void show();
enum { value = 1 };
};

template <typename T>
void A<T>::show(){
if(value == 1)
std::cout << "complex" << std::endl;
else
std::cout << "no complex" << std::endl;
}

/*template <typename T>
void A<std::complex<T> >::show(){
if(value == 1)
std::cout << "complex" << std::endl;
else
std::cout << "no complex" << std::endl;
}*/

int main ( void ) {
A<double> a;
A<std::complex<double> > b;
a.show();
b.show();
}

What about:

#include <iostream>
#include <complex>

template < typename T >
struct check_complex {

enum { value = 0 };

};

template < typename T >
struct check_complex < std::complex< T > > {

enum { value = 1 };

};

template < typename T >
class A {
public:

void show ( void ) {
if ( check_complex<T>::value ) {
std::cout << "complex\n";
} else {
std::cout << "not complex\n";
}
}
};

int main ( void ) {
A<int>().show();
A< std::complex<float> >().show();
}


Best

Kai-Uwe Bux
 

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

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top