You are not logged in.
Pages: 1
The following code works fine with VC express, but not in GCC. Does anyone know why it won't compile? It gives this error:
conversion from 'main()::functor' to non-scalar type 'std::tr1::function<void()>' requested
Same thing happens if I use boost::function instead of std::tr1::function.
#include <iostream>
#include <tr1/functional>
#include <tr1/functional_hash.h>
void callfunc(std::tr1::function<void()> func) {func();}
int main() {
struct functor {
void operator()(void) {std::cout << "lol" << std::endl;}
};
functor f;
callfunc(f);
}
Offline
I don't know the internals of tr1::function, but it seems to me that g++ is complaining about a valid failed type conversion.
Could you try declaring functor as:
struct functor: public std::tr1::function<void()> {
Offline
You suggested:
#include <iostream>
#include <tr1/functional>
#include <tr1/functional_hash.h>
using std::tr1::function;
void callfunc(function<void()> func) {func();}
int main() {
struct functor : public function<void()> {
void operator()(void) {std::cout << "lol" << std::endl;}
};
functor f;
callfunc(f);
}
This happens:
terminate called after throwing an instance of 'std::tr1::bad_function_call' what(): std::exception
Offline
Ah, well it was just the simplest way of avoiding that particular error, short of an explicit cast, which I assumed wouldn't work either.
Edit: could you try moving the struct declaration out of main, instead? I believe gcc's local class declarations are causing the problem here, since according to tr1, your syntax appears perfectly correct.
Last edited by sr (2009-10-13 04:47:17)
Offline
Just to be clear, this works for me:
#include <iostream>
#include <tr1/functional>
void callfunc(std::tr1::function<void()> &func) {func();}
struct functor{
void operator()() {std::cout << "lol" << std::endl;}
};
int main(int, char*[]) {
std::tr1::function<void()> f = functor();
callfunc(f);
}
Moving the struct into main() doesn't, for some reason - at least, I *think* I understand why it doesn't, but don't see why it works that way.
Offline
Nvm, gcc doesn't support it yet.
Offline
Pages: 1