guess you have the following:
_______________ _______________ _______________ ____
template <class T>
class CQVector
{
public:
// find an element, returns index or -1 if none is found
int find(int id) const;
private:
std::vector<Tm_ vec;
};
template <class T>
int CQVector<T>::fi nd(int id) const
{
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt].ID() == id)
break;
}
return iCnt;
}
_______________ _______________ _______________ ____
this finds an element in the vector by calling elements fn ID() and
compiles for all structures/classes that have an ID() memeber-fn
returning something comparable to int.
now i also want the possibility to store pointers, eg
CQVector<MyClas s*m_foo;
and need some specialisation that does a...
_______________ _______________ _______________ ____
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt]->ID() == id)
break;
}
return iCnt;
_______________ _______________ _______________ ____
what syntax is need ed for the specialisation?
something like template<class* Tdoesnt work...
TIA, -.rhavin;) 3 2443
On 7ÔÂ1ÈÕ, ÉÏÎç2ʱ59·Ö, ".rhavin grobert" <cl...@yahoo.de wrote:
guess you have the following:
_______________ _______________ _______________ ____
template <class T>
class CQVector
{
public:
// find an element, returns index or -1 if none is found
int find(int id) const;
private:
std::vector<Tm_ vec;
};
template <class T>
int CQVector<T>::fi nd(int id) const
{
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt].ID() == id)
break;
}
return iCnt;}
_______________ _______________ _______________ ____
this finds an element in the vector by calling elements fn ID() and
compiles for all structures/classes that have an ID() memeber-fn
returning something comparable to int.
now i also want the possibility to store pointers, eg
CQVector<MyClas s*m_foo;
and need some specialisation that does a...
_______________ _______________ _______________ ____
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt]->ID() == id)
break;
}
return iCnt;
_______________ _______________ _______________ ____
what syntax is need ed for the specialisation?
something like template<class* Tdoesnt work...
template <class T>
class CQVector<T*>
{
...
};
".rhavin grobert" <cl***@yahoo.de a écrit dans le message de news: e2************* *************** **...legroup s.com...
guess you have the following:
_______________ _______________ _______________ ____
template <class T>
class CQVector
{
public:
// find an element, returns index or -1 if none is found
int find(int id) const;
private:
std::vector<Tm_ vec;
};
template <class T>
int CQVector<T>::fi nd(int id) const
{
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt].ID() == id)
break;
}
return iCnt;
}
_______________ _______________ _______________ ____
this finds an element in the vector by calling elements fn ID() and
compiles for all structures/classes that have an ID() memeber-fn
returning something comparable to int.
now i also want the possibility to store pointers, eg
CQVector<MyClas s*m_foo;
and need some specialisation that does a...
_______________ _______________ _______________ ____
int iCnt = m_vec.size();
while (iCnt-->0)
{
if (m_vec[iCnt]->ID() == id)
break;
}
return iCnt;
_______________ _______________ _______________ ____
what syntax is need ed for the specialisation?
something like template<class* Tdoesnt work...
TIA, -.rhavin;)
No need to partially specialize your class by the way. Here an example.
#include <boost/utility.hpp>
#include <boost/type_traits.hpp >
using namespace std;
//This template function will be called only if T is a pointer. return type
is int
template<typena me T>
typename boost::enable_i f_c<boost::is_p ointer<T>::valu e, int>::type
Do() {return 1;}
//This template function will be called only if T is NOT a pointer. return
type is int
template<typena me T>
typename boost::disable_ if_c<boost::is_ pointer<T>::val ue, int>::type
Do() {return 2;}
template<typena me T>
class A
{
public:
int DoSomething()
{
return Do<T>(); // Do actually take careof doing the right thing
depending on T
}
};
int main()
{
A<inta1;
A<int*a2;
cout <<a1.DoSomethin g() <<endl; // print 2
cout <<a2.DoSomethin g() <<endl; // print 1
return 0;
}
Ok I agree that the Do<Tfunction template could look a little (ok maybe
not just a little...) strange at first sight but it is not actually that
bad.
take for example
boost::enable_i f_c<boost::is_p ointer<T>::valu e, int>::type
there is 2 template arg to enable_if_c
1.boost::is_poi nter<T>::value
2. int
>
No need to partially specialize your class by the way. Here an example.
#include <boost/utility.hpp>
#include <boost/type_traits.hpp >
using namespace std;
//This template function will be called only if T is a pointer. return
type is int
template<typena me T>
typename boost::enable_i f_c<boost::is_p ointer<T>::valu e, int>::type
Do() {return 1;}
//This template function will be called only if T is NOT a pointer.
return type is int
template<typena me T>
typename boost::disable_ if_c<boost::is_ pointer<T>::val ue, int>::type
Do() {return 2;}
template<typena me T>
class A
{
public:
int DoSomething()
{
return Do<T>(); // Do actually take careof doing the right thing
depending on T
}
};
int main()
{
A<inta1;
A<int*a2;
cout <<a1.DoSomethin g() <<endl; // print 2
cout <<a2.DoSomethin g() <<endl; // print 1
return 0;
}
Ok I agree that the Do<Tfunction template could look a little (ok maybe
not just a little...) strange at first sight but it is not actually that
bad.
take for example
boost::enable_i f_c<boost::is_p ointer<T>::valu e, int>::type
there is 2 template arg to enable_if_c
1.boost::is_poi nter<T>::value
2. int
Sorry, I pushed the wrong button...
ok so boost::is_point er<T>::value will evaluate to true if T is a pointer
and false otherwise.
Inside enable_if_c, there is a simple typedef:
typedef T type;
it happen thet the second argument of the enable_if_c template is T (in our
case int)
so enable_if_c<tru e,int>::type == int and enable_if_c<tru e,MyClass>::typ e
== MyClass, and so on
So we can use the enable_if_c template as the return argument four our Do
function template.
Now what haapen when boost::is_point er<T>::value evaluate to false? It is a
substitution failure (see the SFINAE principle). In short the compiler will
just reject the template and search for a better match (ok, if he does not
find a better match he will give you an error). In our case, the second Do
function will match for every non pointer type.
In your case, you seem to have only 1 function to "specialize "and it it is a
really simple function, so if you can install boost ( www.boost.org) you
should consider this option.
----------------------
Eric Pruneau This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Agent Mulder |
last post by:
Hi group,
I have a problem with partial template specialization. In the code
below I have a template struct Music with one method, play(),
and three kinds of music, Jazz, Funk and Bach. When I specialize
Music<Bach>, I expect that the original play() method is available
in the specialization, but it is not. How can I fix this?
-X
|
by: Patrick Kowalzick |
last post by:
Dear all,
the following code is illegeal (but compiles with MSVC 7.1):
// *** illegal ***
struct outer0 {
template<typename inner_var>
struct inner { };
template<>
|
by: Rafal Dabrowa |
last post by:
Consider the following code:
template <class T>
void f(T, int) {}
template <class T>
void f(int, T) {}
template<>
void f(int, int) {} // ambiguous ?
My compiler complains that the last definition is an "ambiguous template
specialization `f<>' for `void f(int, int)' ". I have tested this on a
|
by: SoilMan |
last post by:
Consider the following:
class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}
|
by: Marek Vondrak |
last post by:
Hello.
I have written the following program and am curious why it prints "1" "2".
What are the exact effects of explicitly providing function template
parameters at the call? Is the second assign() function really a
specialization of the first assign() or is it an assign() overload?
Thank you.
-- Marek
| |
by: Bari |
last post by:
Hi,
When i do the following specializition which commented in the source then
including the header in two cpp files gives me an error:
test3.obj : error LNK2005: "class A<int* __cdecl makeA<int>(void)"
(??$makeA@H@@YAPAV?$A@H@@XZ) already defined in testSTL.obj
Can you please tell me how i can specialize the template function and still
use the header in multiple cpp files.
|
by: Barry |
last post by:
The following code compiles with VC8
but fails to compiles with Comeau online,
I locate the standard here:
An explicit specialization of any of the following:
|
by: mike b |
last post by:
Hello everyone, thanks in advance for your help. I'm new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I'm
trying to use from an executable, compile using gcc 4.1.2. Everything
works fine until I try specializing one of the static member function
templates in a non-template class. I have a feeling I'm messing up
something obvious so before I post a...
|
by: mathieu |
last post by:
Hi there,
I know this is not possible in c++. So my question, how should I
rewrite the following piece of code (without using a dummy class which
template parameter could be use for partial specialization).
template <typename TOut, typename TIn>
void InverseRescaleFunction(TOut *out, const TIn *in, double
intercept, double slope, size_t size)
{ ... }
|
by: (2b|!2b)==? |
last post by:
I have a class template. Each of the instantiations implements a method
in the class template differently, so I (need?) to use template
speciaization.
My question is this, when writing the specialization, do I need to
implement only the method that is 'different', or do I need to implement
all the methods in the class template?
template <typename T1, typename T2>
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |