Шаблонные функции и boost::bind
Привет,
пытаюсь забиндить статическую шаблонную функцию и получаю какие-то странные сообщения об ошибке от MSVC9:
1>c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t esttemplate.cpp(23) : error C2064: term does not evaluate to a function taking 0 arguments
1> c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t esttemplate.cpp(32) : see reference to function template instantiation 'void B::f<C*,void,C>(int C::* ,A)' being compiled
1> with
1> [
1> A=C *
1> ]
откуда он там взял int C::* я не понимаю. И как исправить ошибку пока тоже не могу понять. У кого-нибудь есть мысли по этому поводу?
Под катом собственно код.
gcc говорит просто
a.cpp: In function 'int main(int, char**)':
a.cpp:34: error: address of overloaded function with no contextual type information
#include <iostream>
#include "boost/bind.hpp"
class C
{
public:
void h()
{
std::cout << "cool" << std::endl;
}
};
class B
{
public:
template<typename A, typename T, typename M>
static void f(T M::*g, A a);
};
template<typename A, typename T, typename M>
void B::f(T M::*g, A a)
{
a->*g();
}
typedef void (C::*Func1)();
typedef void (*Func2)(Func1, C*);
int main(int argc, char* argv[])
{
C c;
boost::bind((Func2)&B::f<C *, void, C>, &C::h, &c);
return 0;
}
UPDATE наверно правильно всё-таки так:
boost::bind(&B::f<C *, void (C::*)(), C>, &C::h, &c);
но ошибка остается примерно такая же, хотя int там уже нет:
c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t esttemplate.cpp(23) : error C2064: term does not evaluate to a function taking 0 arguments
1> c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t esttemplate.cpp(32) : see reference to function template instantiation 'void B::f<C*,void(__thiscall C::* )(void),C>(T C::* ,A)' being compiled
1> with
1> [
1> T=void (__thiscall C::* )(void),
1> A=C *
1> ]
UPDATE2 gcc с заменой соответствующих строчек на
(a->*g)();
и
boost::bind(&B::f<C *, void (C::*)(), C>, &C::h, &c);
выдает другое сообщение об ошибке:
a.cpp: In static member function 'static void B::f(T M::*, A) [with A = C*, T = void (C::*)(), M = C]':
a.cpp:36: instantiated from here
a.cpp:23: error: must use '.*' or '->*' to call pointer-to-member function in '*((& * a) + ((int)g)) (...)'
версия gcc 4.1.2
Спасибо :)
UPDATE3 проблема решена.
надо писать
boost::bind(&B::f<C *, void (), C>, &C::h, &c);
спасибо
esil0x за то, что навел на ответ
пытаюсь забиндить статическую шаблонную функцию и получаю какие-то странные сообщения об ошибке от MSVC9:
1>c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t
1> c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t
1> with
1> [
1> A=C *
1> ]
откуда он там взял int C::* я не понимаю. И как исправить ошибку пока тоже не могу понять. У кого-нибудь есть мысли по этому поводу?
Под катом собственно код.
gcc говорит просто
a.cpp: In function 'int main(int, char**)':
a.cpp:34: error: address of overloaded function with no contextual type information
#include <iostream>
#include "boost/bind.hpp"
class C
{
public:
void h()
{
std::cout << "cool" << std::endl;
}
};
class B
{
public:
template<typename A, typename T, typename M>
static void f(T M::*g, A a);
};
template<typename A, typename T, typename M>
void B::f(T M::*g, A a)
{
a->*g();
}
typedef void (C::*Func1)();
typedef void (*Func2)(Func1, C*);
int main(int argc, char* argv[])
{
C c;
boost::bind((Func2)&B::f<C *, void, C>, &C::h, &c);
return 0;
}
UPDATE наверно правильно всё-таки так:
boost::bind(&B::f<C *, void (C::*)(), C>, &C::h, &c);
но ошибка остается примерно такая же, хотя int там уже нет:
c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t
1> c:\documents and settings\kstepanenko\my documents\visual studio 2008\projects\testtemplate\testtemplate\t
1> with
1> [
1> T=void (__thiscall C::* )(void),
1> A=C *
1> ]
UPDATE2 gcc с заменой соответствующих строчек на
(a->*g)();
и
boost::bind(&B::f<C *, void (C::*)(), C>, &C::h, &c);
выдает другое сообщение об ошибке:
a.cpp: In static member function 'static void B::f(T M::*, A) [with A = C*, T = void (C::*)(), M = C]':
a.cpp:36: instantiated from here
a.cpp:23: error: must use '.*' or '->*' to call pointer-to-member function in '*((& * a) + ((int)g)) (...)'
версия gcc 4.1.2
Спасибо :)
UPDATE3 проблема решена.
надо писать
boost::bind(&B::f<C *, void (), C>, &C::h, &c);
спасибо
