我需要调用一个需要函数指针的方法,但我真正想传递给它的是一个仿函数.这是我正在尝试做的一个例子:
#include <iostream>
#include "boost/function.hpp"
typedef int (*myAdder)(int);
int adderFunction(int y) { return(2 + y); }
class adderClass {
public:
adderClass(int x) : _x(x) {}
int operator() (int y) { return(_x + y); }
private:
int _x;
};
void printer(myAdder h, int y) {
std::cout << h(y) << std::endl;
}
int main() {
myAdder f = adderFunction;
adderClass *ac = new adderClass(2);
boost::function1<int, int> g =
std::bind1st(std::mem_fun(&adderClass::operator()), ac);
std::cout << f(1) << std::endl;
std::cout << g(2) << std::endl;
printer(f, 3); …Run Code Online (Sandbox Code Playgroud) 以下代码尝试根据成员函数指针类型的返回类型来专门化类模板'special',导致VC9编译错误:
template<class F> struct special {};
template<class C> struct special<void(C::*)()> {};
template<class R, class C> struct special<R(C::*)()> {};
struct s {};
int main()
{
special<void(s::*)()> instance;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误C2752:'special':多个部分特化匹配模板参数列表
GCC-4.3.4接受相同的代码,如下所示:http ://ideone.com/ekWGg
这是VC9中的一个错误,如果是这样,这个错误是否仍然存在于VC10中?
然而,我提出了一个可怕的侵入式解决方法(对于这个特定的用例,至少.欢迎更一般的解决方案):
#include <boost/function_types/result_type.hpp>
#include <boost/type_traits/is_same.hpp>
template<typename F, typename R>
struct is_result_same :
boost::is_same<
typename boost::function_types::result_type<F>::type,
R
>
{};
template<class F, bool = is_result_same<F, void>::value>
struct special {};
template<class R, class C> struct special<R(C::*)(), true> {};
template<class R, class C> struct special<R(C::*)(), false> {};
Run Code Online (Sandbox Code Playgroud) c++ templates member-function-pointers partial-specialization visual-c++
在我的代码中,我有一个类,它注册其他类的方法:
#include <iostream>
using namespace std;
template< typename C>
class Reg {
public:
template< typename R, typename A>
void register_f( string name, R ( C:: *method_p ) ( A)) { /*registration process*/ }
// template< typename R>
// void register_void_f( string name, R ( C:: *method_p ) ( void)) { /*registration process*/ }
};
class A {
public:
int f( void) { return 1; }
void g( int x) { /* some code*/ }
};
int main() {
Reg< A> …Run Code Online (Sandbox Code Playgroud) 我需要使用一个成员函数指针,它接受在其他代码中使用的基类的参数.好吧,我只想做[某事]如下面的例子.这段代码工作正常,但我想知道这样的演员是否总是安全的?我不能在这里做dynamic或static投.
#include <cstdio>
class C
{
public:
C () : c('c') {}
virtual ~C() {}
const char c;
};
class D : public C
{
public:
D () : d('d') {}
virtual ~D() {}
const char d;
};
class A
{
public:
A () {}
virtual ~A() {}
void f( C& c ) { printf("%c\n",c.c); }
void g( D& d ) { printf("%c %c\n",d.c,d.d); }
};
int main (int argc, char const* argv[])
{
void (A::*pf)( …Run Code Online (Sandbox Code Playgroud) c++ pointers casting member-function-pointers reinterpret-cast
以下代码产生编译时错误:
'
base::print':无法访问类中声明的私有成员'base_der'
但是,我已public在派生类中创建了该成员.为什么这不起作用?
#include <iostream>
using namespace std;
class base
{
public:
int i;
void print(int i)
{
printf("base i\n");
}
};
class base_der : private base
{
public:
using base::print;
};
int main()
{
// This works:
base_der cls;
cls.print(10);
// This doesn't:
void (base_der::* print)(int);
print = &base_der::print; // Compile error here
}
Run Code Online (Sandbox Code Playgroud) c++ member-function-pointers member-pointers implicit-conversion private-inheritance
是否可以声明一个可以指向任何类的成员函数的函数指针(非 C++ 11)(阅读:不是特定的类)?
例如,如果我有类 A、B 和 C。C 中声明了一个函数指针,我想在指向 B 的成员函数之一和 A 的成员函数之一之间切换该指针。C++ 允许这样做吗?
我有一个课程如下
class A
{
public:
A(int key) : m_key(key) {}
int Key() const {return m_key;}
private:
int m_key;
};
Run Code Online (Sandbox Code Playgroud)
我使用带有成员函数指针的unique_ptr进行测试
int (A::*MemFun)() const;
MemFun = &A::Key;
( std::unique_ptr<A>(new A(10)) ->*MemFun ) (); // Error C2296
( std::unique_ptr<A>(new A(10)).get() ->*MemFun ) (); // okay
(*std::unique_ptr<A>(new A(10)) .*MemFun ) (); // okay
Run Code Online (Sandbox Code Playgroud)
第一个给出编译错误(VC2010给出错误C2296,非法,左运算符包括std :: unique_ptr <_Ty>).为什么?谢谢.
在处理类成员函数指针时,我们可以使用以下语法在对象实例上调用函数:
struct X {
void foo();
};
X x; // Instance
auto f = &X::foo; // Member function pointer
(x.*f)(); // Call function f on x
Run Code Online (Sandbox Code Playgroud)
当有一个实例的原始指针时,语法是这样的:
X *xptr = new X();
(xptr->*f)();
Run Code Online (Sandbox Code Playgroud)
这很好地遵循了静态函数调用的类比:
x.foo();
x->foo();
Run Code Online (Sandbox Code Playgroud)
但是,当我有一个类的智能指针时,这不起作用:
unique_ptr<X> xsptr(new X());
(xsptr->*f)(); // g++: error: no match for 'operator->*' in 'xsptr ->* f'
Run Code Online (Sandbox Code Playgroud)
我通过首先应用取消引用运算符,然后使用以下.语法调用来解决此问题:
((*xsptr).*f)();
Run Code Online (Sandbox Code Playgroud)
这是有多丑?
编译器是否应该拒绝上面的箭头语法?因为通常(静态调用函数时),它调用operator ->. 不应该->*也只是打电话operator ->吗?
编译器错误部分地回答了这个问题:错误读作我们可以重载operator ->*以调用解除引用对象上的成员函数指针,但unique_ptr事实并非如此。但这带来了更多的问题。如果语言需要这样做,为什么智能指针不这样做?是不是打算不这样做,因为它引入了其他问题?为什么我们必须写这个操作符,而不是隐式调用返回的对象上的成员函数指针->?(因为这是写作时的行为xsptr->foo() …
c++ member-function-pointers smart-pointers operator-overloading c++11
我知道这已经是一个长期讨论的话题,但我还没有找到满足我的答案.
简而言之:即使使用C++ 11的function :: target()功能,是不是可以将成员函数指针传递给c风格的方法?
以下代码不起作用:对mbf.target()的调用将返回0,从而生成SEGFAULT.我不明白为什么,因为我将成员函数绑定到泛型函数对象,所以类型应该没问题.
我做错了什么或者我想做一些不可能的事情?
#include <functional>
using namespace std;
typedef void (*CBType)(int, int);
CBType myCB = 0;
void regCallback(CBType cb)
{
myCB = cb;
}
class A
{
public:
void memberCB(int a, int b) {}
void registerCallback()
{
auto mbMem = mem_fn(&A::memberCB);
function<void(int,int)> mbf =
bind(mbMem, *this, placeholders::_1, placeholders::_2);
regCallback(*mbf.target<void(*)(int,int)>());
}
};
int main()
{
A inst;
inst.registerCallback();
}
Run Code Online (Sandbox Code Playgroud) 我试图fn根据某个constexpr值选择一个成员。然后我尝试调用选定的函数,但是我收到了关于如何fn使用不正确的语法调用成员的错误。
error: must use '.*' or '->*' to call pointer-to-member function in
'S::SelectedGetter<&S::fn1, &S::fn2>::fn (...)', e.g. '(... ->*
S::SelectedGetter<&S::fn1, &S::fn2>::fn) (...)'
18 | return SelectedGetter<&S::fn1, &S::fn2>::fn();
Run Code Online (Sandbox Code Playgroud)
我试图称之为“正确”但失败了。最后我使用的是std::invoke,但我想知道这是否可以在没有 的情况下完成std::invoke,只使用“原始”C++ 语法。
#include <algorithm>
#include <type_traits>
static constexpr int number = 18;
struct S
{
using GetterFn = uint32_t(S::*)() const;
uint32_t fn1()const {
return 47;
}
uint32_t fn2() const {
return 8472;
}
template <GetterFn Getter1, GetterFn Getter2>
struct SelectedGetter
{
static constexpr …Run Code Online (Sandbox Code Playgroud)