473,836 Members | 1,983 Online

# 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++;
}
}
};
>

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
--
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 (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, 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 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 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