generic classes template function with function pointer and argumentnumber overloading (lol)

G

g3rc4n

help me, i could of sworn this was working a while ago, reason i ask
for help is that i don't trust vc++ errors

i'm trying to write a generic observer-subject class where a fucntion
pointer with variable number of arguments are passed then executed of
each element

also if anyone can think of a way round the macro without code
duplication your welcome to comment on the use of a "evil" macro in c+
+

#include <vector>
#include <functional>
#include <iostream>

#define GENERIC_IMPL_ITERATE__ \
for(VI ITER__(subscribers_.begin()),END__(subscribers_.end()); \
ITER__!=END__; \
++ITER__)

template<class T>
struct generic_impl{
typedef std::vector<T*> vector_t;
typedef typename vector_t::iterator VI;

void on_eachh(void* fun){
typedef void(*fun_t)(T*);
GENERIC_IMPL_ITERATE__{
(reinterpret_cast<fun_t>(fun))(*ITER__);
}
}
template<typename FUN>
void on_each(FUN fun){
GENERIC_IMPL_ITERATE__{
fun(*ITER__);
}
}

template<typename FUN, typename A>
void on_each(FUN fun, A& a){
GENERIC_IMPL_ITERATE__{
fun(*ITER__,a);
}
}
template<typename FUN, typename A, typename B>
void on_each(FUN fun, A& a, B& b){
GENERIC_IMPL_ITERATE__{
fun(*ITER__,a,b);
}
}
void subscribe(T* t){
subscribers_.push_back(t);
}
private:
vector_t subscribers_;
};

#undef GENERIC_IMPL_ITERATE__

struct foo{
void zero(){}
void one(char){}
void two(char,char){}
};

int main(){
generic_impl<foo> impl;
impl.on_each(reinterpret_cast<void*>(&foo::zero));
impl.on_each(std::mem_fun(&foo::zero));
impl.on_each(std::mem_fun(&foo::eek:ne),'a');
return(0);
}
 
A

Alf P. Steinbach

* (e-mail address removed):
help me, i could of sworn this was working a while ago, reason i ask
for help is that i don't trust vc++ errors

i'm trying to write a generic observer-subject class where a fucntion
pointer with variable number of arguments are passed then executed of
each element

also if anyone can think of a way round the macro without code
duplication your welcome to comment on the use of a "evil" macro in c+
+

#include <vector>
#include <functional>
#include <iostream>

#define GENERIC_IMPL_ITERATE__ \
for(VI ITER__(subscribers_.begin()),END__(subscribers_.end()); \
ITER__!=END__; \
++ITER__)

Identifiers with double underscores are reserved to the implementation.

Presumably you're using some names from your standard library implementation.

Don't.

template<class T>
struct generic_impl{
typedef std::vector<T*> vector_t;
typedef typename vector_t::iterator VI;

void on_eachh(void* fun){
typedef void(*fun_t)(T*);
GENERIC_IMPL_ITERATE__{

At this point there's no ITER__ or END__ in sight.

Are you sure they're defined?


Cheers & hth.,

- Alf
 
G

g3rc4n

* (e-mail address removed):








Identifiers with double underscores are reserved to the implementation.

Presumably you're using some names from your standard library implementation.

Don't.



At this point there's no ITER__ or END__ in sight.

Are you sure they're defined?

Cheers & hth.,

- Alf

i thought it was only names that began with double underscores?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top