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

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

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();
}

Jul 23 '05 #1
7 1431
Pe*******@gmail.com wrote:
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();
}


Jul 23 '05 #2
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();
}

Jul 23 '05 #3
Pe*******@gmail.com wrote:
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
Jul 23 '05 #4
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();
}

Jul 23 '05 #5
Pe*******@gmail.com wrote:
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.

Jul 23 '05 #6
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

Jul 23 '05 #7
Pe*******@gmail.com wrote:
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
Jul 23 '05 #8

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

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
3
by: Bolin | last post by:
I am using a macro to define template functions that look the same except for a type. However when the return type it a template class with several template arguments (say, A<T1, T2>) then the...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
3
by: danilo.horta | last post by:
Hi guys I'm trying to accomplish a slightly difficult task. I think it's more easy to explain trought an unworking code: template<class T, size_t numDim> VecBasis {/*...*/}; typedef...
4
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
1
by: newbie | last post by:
Here is my question: I want to have a class, which requires a typename T with interface "operate()" and "Init()" as its parameter. template <class Tclass Foo { public: Foo(int param) {...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
2
by: ds | last post by:
Hi all, what I try to do is the following: template<class Tpclass themap { public: typedef int Tp::*ptr; static std::map<std::string, Tp::ptrsmap;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.