473,836 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Algorithm template specialization problem

Hi all,

I'm trying to write a generic algorithm routine that will
take begin and end iterators of a container, iterate through
the range and perform a "calculatio n" of sorts.

The trouble is that the algorithm will behave differently for
the two different types. I've considered calling the algorithm
foo_A and foo_B, but I don't like that approach because it will
blow out in naming complexity down the track.

I was hoping with partial specialisations , I could get something
happening, but I'm finding it difficult templating an iterator but
not knowing what "type" of iterator it is. Also another major
problem is the algorithm wont know what kind of type the classes
A or B will be specialised as.

I've considered using a function object, but the algorithm I'm trying
to perform is not a for_each style thing, it needs to go through all
the elements in the iterator range, do some things, then modify the
elements accordingly.

Some of the problem is see are:

1. Can't specialize on A or B properply without knowing their types
2. It would be unprofessional to have foo<T,A<T(v.beg in,v.end)

below is what I've got so far, the code it pretty self explanatory,
but mind you it wont compile without errors.
template<typena me T>
class A { public: T val; };

template<typena me T>
class B { public: T val1; T val2; };

template< typename T,
typename P,
typename Iterator class foo;
template<typena me T, typename P = A<T>, typename Iterator>
class foo
{
void operator()(Iter ator begin, Iterator end)
{
for(Iterator it = begin, it != end; it++)
{
(*it).val++;
}
}
};
template< typename T, typename P = B<T>, typename Iterator>
class foo
{
void operator()(Iter ator begin, Iterator end)
{
for(Iterator it = begin, it != end; it++)
{
(*it).val1++;
(*it).val2++;
}
}
};
int main(void)
{
std::vector< A<double vec1(100,1);
foo< A<double()(vec1 .begin(),vec1.e nd());

std::vector< B<double vec2(100,1);
foo< B<double()(vec2 .begin(),vec2.e nd());

return true;
}
I was hoping someone could give me some good ideas, tips
or a way to get what I want happening. I would appreciate
it very much.


Sherrie

Jul 26 '06 #1
2 2151
Sherrie Laraurens wrote:
I'm trying to write a generic algorithm routine that will
take begin and end iterators of a container, iterate through
the range and perform a "calculatio n" of sorts.

The trouble is that the algorithm will behave differently for
the two different types. I've considered calling the algorithm
foo_A and foo_B, but I don't like that approach because it will
blow out in naming complexity down the track.

I was hoping with partial specialisations , I could get something
happening, but I'm finding it difficult templating an iterator but
not knowing what "type" of iterator it is. Also another major
problem is the algorithm wont know what kind of type the classes
A or B will be specialised as.

I've considered using a function object, but the algorithm I'm trying
to perform is not a for_each style thing, it needs to go through all
the elements in the iterator range, do some things, then modify the
elements accordingly.
Sounds like a 'transform' analogue.
Some of the problem is see are:

1. Can't specialize on A or B properply without knowing their types
2. It would be unprofessional to have foo<T,A<T(v.beg in,v.end)

below is what I've got so far, the code it pretty self explanatory,
but mind you it wont compile without errors.
template<typena me T>
class A { public: T val; };

template<typena me T>
class B { public: T val1; T val2; };

template< typename T,
typename P,
typename Iterator class foo;
template<typena me T, typename P = A<T>, typename Iterator>
You need to either move 'P' to the end or to give 'Iterator' its
default argument. I think you actually meant to specialise 'foo',
so here is what it should looke like:

template<typena me T, typename Iterator>
class foo<T,A<T>,Iter ator>
class foo
{
void operator()(Iter ator begin, Iterator end)
{
for(Iterator it = begin, it != end; it++)
{
(*it).val++;
}
}
};
template< typename T, typename P = B<T>, typename Iterator>
Same default argument problem here.
class foo
{
void operator()(Iter ator begin, Iterator end)
{
for(Iterator it = begin, it != end; it++)
{
(*it).val1++;
(*it).val2++;
}
}
};
Add here
>

int main(void)
{
std::vector< A<double vec1(100,1);
foo< A<double()(vec1 .begin(),vec1.e nd());

std::vector< B<double vec2(100,1);
foo< B<double()(vec2 .begin(),vec2.e nd());

return true;
}
I was hoping someone could give me some good ideas, tips
or a way to get what I want happening. I would appreciate
it very much.
I don't know if it's going to give you any ideas (I hope it doesn't give
you the wrong ones :-)) but it compiles for me on VC++ 2005 (although does
give some problems with Comeau online test:
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
template<typena me T>
class A { public: T val; A(T t) : val(t) {} };

template<typena me T>
class B { public: T val1; T val2; B(T t) : val1(t), val2(0) {} };

template<typena me Iterator, typename Vclass foo;

template<typena me Iterator, typename T>
class foo<Iterator, A<T
{
public:
void operator()(Iter ator begin, Iterator end)
{
for (Iterator it = begin; it != end; it++)
{
(*it).val++;
}
}
};

template<typena me Iterator, typename T>
class foo<Iterator, B<T
{
public:
void operator()(Iter ator begin, Iterator end)
{
for(Iterator it = begin; it != end; it++)
{
(*it).val1++;
(*it).val2++;
}
}
};

template<typena me Iterator>
void do_foo(Iterator it1, Iterator it2)
{
foo<Iterator, Iterator::value _typeft;
ft(it1, it2);
}

#include <vector>

int main()
{
std::vector< A<double vec1(100,1);
do_foo(vec1.beg in(),vec1.end() );

std::vector< B<double vec2(100,1);
do_foo(vec2.beg in(),vec2.end() );

return true;
}
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '06 #2
Maybe the following technique can help a bit:

class A{};
class B{};

template <typename T1, typename T2>
struct same_type
{
enum {result = 0};
};

template <typename T>
struct same_type<T, T>
{
enum {result = 1};
};

template <typename Itr>
void foo(Itr i, Itr f)
{
typedef
typename std::iterator_t raits<Itr>::val ue_type
val_t;

if (same_type<val_ t, A>::result) // specialize for type A
{
// ...
}
else if (same_type<val_ t, B>::result) // specialize for B
{
// ...
}
else // non-specialized
{
// ...
}
}
Regards,
Ben
Jul 27 '06 #3

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

Similar topics

17
6870
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
6
3356
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something like that possible? Some workaround? Thank you, Patrick
2
2545
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
8
7692
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
2
5782
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
31
3547
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
5
1720
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
17
6526
by: silverburgh.meryl | last post by:
In STL, there is no copy_if algorithm. But how can I emulate that? I am thinking about using find_if and then a copy. But how can I pass the InputIterator (begin and end) to copy so that it knows the range it tries to copy from. Thanks for any idea.
6
2623
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization, one for std::vector<T,Aconst & non const other for std::deque<T,Aconst & non const.
0
9812
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, 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...
0
9658
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,...
0
10534
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 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...
1
10579
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9364
agi2029
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...
0
6975
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();...
0
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4444
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
2
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.