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<T p> >
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();
} 7 1451 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<T p> > 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<typena me 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(); }
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();
} 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
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();
} 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.
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 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<fl oat> >().show();
}
Best
Kai-Uwe Bux This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: s |
last post by:
Can I do this:
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING;
..
..
..
|
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 6.0-7.1).. Note, that basically I don't
need an "extern C" linkage,
I just need to define a type to cast a resolved function address..
Any ideas?
Basically , I need something like this..
|
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 macro does
not work because of the comma -- the macro thinks there are more
arguments provided since the comma separates arguments. To avoid the
comma being taken into account the usual way is to put parenthesis,
but here it would not work because...
|
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 of any friend function defined in that
template (unless we want to prevent more than one instantiation of a
class template in a particular file, but this is rather unlikely).
Let's apply this to a variation of our previous example:
|
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 VecBasis<float, 3> vec3;
template<class T, size_t size> class MatBasis {
//...
| |
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);
};
template<typename T>
|
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) { _algorithm.Init(param); }
void do_algorithm(vector<inta) {
_algorithm.operate(a);
....
|
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:
const char *s = string_mapper<thetype>();
However I do not know the string to be associated with the type at
compile time, and will need a way to set up the mapping, to be created
at run time, possibly like so:
void foo(char*...
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |