是否可以T从其指向memmber的指针推断出类的类型,T::*f如下所示.
struct Foo
{
    void func(){}
};
template<typename T, void (T::*f)()>
void bar()
{
}
int main()
{
    bar<Foo,Foo::func>();
    // bar<Foo::func>(); // Desired
}
c++ templates pointer-to-member type-deduction template-argument-deduction
以下代码不起作用,但它表达了我想做的事情.模板结构容器存在问题,我认为应该这样做,因为它的大小对于任何模板参数都是已知的.
class callback {
  public:
  // constructs a callback to a method in the context of a given object
  template<class C>
  callback(C& object, void (C::*method)())
    : ptr.o(object), ptr.m(method) {}
  // calls the method
  void operator()() {
    (&ptr.o ->* ptr.m) ();
  }
  private:
  // container for the pointer to method
  template<class C>
  struct {
    C& o;
    void (C::*m)();
  } ptr;
};
有没有办法做这样的事情?我的意思是有一个非模板类回调,它包装任何指向方法的指针?
谢谢C++大师!
编辑:
请看这个:
以下回调类是"可调用事物"的通用包装器.我真的很喜欢它的API,它没有模板而且非常干净,但是引擎盖下有一些我无法避免的动态分配.
在维护回调类的语义和API的同时,有没有办法摆脱下面的代码中的new和delete?我真的希望我能.
需要的东西:
// base class for something we can "call"
class callable {
  public:
  virtual void operator()() = 0;
  virtual ~callable() {}
};
// wraps pointer-to-members
template<class C>
class callable_from_object : public callable {
  public:
  callable_from_object(C& object, void (C::*method)())
         : o(object), m(method) {}
  void operator()() {
    (&o ->* m) ();
  }
  private:
  C& o;
  void (C::*m)();
};
// wraps pointer-to-functions or pointer-to-static-members
class callable_from_function : public callable {
   public:
   callable_from_function(void (*function)())
         : f(function) {}
   void …我有以下定义和原型(它们是类的成员函数),我基本上尝试使用函数指针将不同类型的策略传递给divideQuery方法:
typedef vector<ConstraintManager> (*strategyType1)(const Query&);
typedef vector<ConstraintManager> (*strategyType2)(const Query&, int);
vector<ConstraintManager> divideQuery (strategyType1 s, const Query& query);
vector<ConstraintManager> divideQuery (strategyType2 s, const Query& query, int parts);
vector<ConstraintManager> divideByHalf(const Query& query);
vector<ConstraintManager> divideRandom(const Query& query);
vector<ConstraintManager> divideByN(const Query& query, int n);
但是当我尝试调用(query参数从包装函数传递)时:
vector<ConstraintManager> result =  divideQuery(divideRandom, query);
它失败并显示错误消息:
DividingSolver.cpp:200:70: error: no matching function for call to ‘DividingSolver::divideQuery(<unresolved overloaded function type>, const klee::Query&)’
DividingSolver.cpp:82:27: note: candidates are: std::vector<klee::ConstraintManager>   DividingSolver::divideQuery(std::vector<klee::ConstraintManager> (*)(const klee::Query&),  const klee::Query&)
DividingSolver.cpp:87:27: note: std::vector<klee::ConstraintManager>     DividingSolver::divideQuery(std::vector<klee::ConstraintManager> (*)(const klee::Query&, int), …我一直在尝试一点SFINAE来确定泛型类型T是否有我可以使用的复制构造函数.这是我现在的位置.
template <bool statement, typename out>
struct Failable
{
    typedef out Type;
};
//This class is only used to insert statements that 
//could encounter substitution failure
template <typename O>
struct COPY
{
    template <typename T>
    typename Failable<true == sizeof(&T::T(const T&)), char>::Type copy(int)
    {}
    template <typename T>
    typename Failable<true, int>::Type copy(...)
    {}
};
然而,这也是我有点卡住的地方.  &T::T(const T&)显然是一个无效的语句,因为我们不能提供带有指向成员的参数列表,即使是ptm函数也是如此.我总是可以尝试指定某种类型void (T::*ptmf)(const T&) = &T::T,并希望它隐式确定正确的重载构造函数放入指向成员函数的指针,但这也意味着构造函数具有特定的返回类型,我必须指定.
有没有其他人有任何我可以跋涉的想法?(我还需要应用类似的概念来检查赋值运算符.)
提前致谢.
通过C++中的共享对象访问成员变量的安全方法是什么?
在下面的代码中,我创建一个共享变量,然后指向它的成员变量.但是,use_count保持不变,当我重置共享变量时,原始变量被重置但不是指向成员的指针.
换句话说,我可以通过使用b来介绍一些错误.它指向的对象不再存在.
#include <iostream>
#include <memory>
using namespace std;
struct A
{
    int y;
    A(int x)
    {
        y = x;
    }
};
int main()
{
    auto a = make_shared<A>(1);
    cout << "a count: " << a.use_count() << endl; //prints a count: 1
    auto b = a->y;
    cout << "a count: " << a.use_count() << endl; //still prints a count: 1
    a.reset();
    cout << "a value: " << a << endl; //prints a value: 0
    cout << "b value: " …  struct MyStruct {
    typedef int MyStruct::*Ptr;  // my pointer-to-member type
    int foo, bar;
  };
这段代码有效,但我想用现代风格更新它,并typedef用a 代替using.什么是正确的语法使用?我尝试了很多选择而且我被卡住了; 我能找到的唯一例子是指向成员函数,这是不同的.
我尝试使用 pybind11 绑定静态重载函数,但遇到了一些问题。
\n\n这是示例代码
\n\n#include <pybind11/pybind11.h>\n\nnamespace py = pybind11;\n\nclass TESTDB {\n  public:\n    static void aaaa(int a, int b) {printf("aaaaa");};\n    static void aaaa(int a) {printf("xxxxx");};\n};\n\nPYBIND11_MODULE(example, m) {\n\n\n  py::class_<TESTDB>(m, "db")\n     .def_static("aaaa", (void (TESTDB::*)(int, int)) &TESTDB::aaaa);\n\n}\n但由于以下原因无法编译
\n\nerror: no matches converting function \xe2\x80\x98aaaa\xe2\x80\x99 to type \xe2\x80\x98void (class TESTDB::*)(int, int)\xe2\x80\x99\n   .def_static("aaaa", (void (TESTDB::*)(int, int)) &TESTDB::aaaa);\n note: candidates are: static void TESTDB::aaaa(int)\n static void aaaa(int a) {printf("xxxxx");};\n note:                 static void TESTDB::aaaa(int, int)\n static void aaaa(int a, int b) {printf("aaaaa");};\n任何想法? …
我有这个场景:
#include <iostream>
class SomeClass
{
public:
    int _int;
};
#define DO_SOME_STUFF(ptr) std::cout << /* Print the typeid().hash_code() of the type which ptr is poiting to (int) */;
int main()
{
    int SomeClass::* ptr_to_int_member = &SomeClass::_int;
    DO_SOME_STUFF(ptr_to_int_member)
}
我想知道ptr指向的是哪种类型(当前是int)。知道哪个类拥有这int也很有用(目前是SomeClass)。
我想创建一个函数对象类,它可以按需在数字上集成一些函数。它的公共接口将包含一个void T::step(double dt)计算积分步骤的方法(并用新值更新函数对象的内部状态)和当前计算值的获取器。
虽然我确信这可以通过纯独立函数来完成,但我将要集成的函数有许多共同的中间计算步骤,并且依赖于许多内部参数,这些参数必须在每个步骤中更新(并且在很大程度上与用户),所以我想到使用函数对象来封装这个机制。
class myIntegrals
{
public:
  void step(double dt);
  double get_f1_integral() { return f1_integral; };
  double get_f2_integral() { return f2_integral; };
private:
  // funcs to be integrated (n.b. they share parameter/return types)
  double f1(double t); // depends on the state of member attributes;
  double f2(double t); // idem, and also on the results of f1
  double t;
  double f1_integral;
  double f2_integral;
  // and many internal parameters, updated on every step
}
void myIntegrals::step(double dt)
{
  // …c++ templates pointer-to-member higher-order-functions c++11
c++ ×10
templates ×4
c++11 ×2
callback ×2
pointers ×1
pybind11 ×1
python ×1
sfinae ×1
shared-ptr ×1