member-functions

std::mem_fun vs std::mem_fn

拜拜、爱过 提交于 2019-12-17 21:55:46
问题 What is the difference between std::mem_fun and std::mem_fn ? Why is the naming so confusing? Boost's documentation says that std::mem_fn can replace std::mem_fun in most cases. So in what situation would you still use std::mem_fun ? 回答1: std::mem_fun is deprecated. std::mem_fn can do everything it does, and it does it more conveniently. The relation between the two is the same as the relation between std::bind1st / std::bind2nd and the C++11 std::bind . Both std::mem_fn and std::bind were

Get memory address of member function?

老子叫甜甜 提交于 2019-12-17 15:35:01
问题 How do I get the absolute address of a member function in C++? (I need this for thunking.) Member function pointers don't work because I can't convert them to absolute addresses ( void * ) -- I need to know the address of the actual function in memory, not simply the address relative to the type. 回答1: There exists a syntax to get the address of the member function in MSVC (starting from MSVC 2005 IMHO). But it's pretty tricky. Moreover, the obtained pointer is impossible to cast to other

Why can some operators only be overloaded as member functions, other as friend functions and the rest of them as both?

戏子无情 提交于 2019-12-17 15:29:29
问题 Why can some operators only be overloaded as member functions, other as non-member "free" functions and the rest of them as both? What is the rationale behind those? How to remember which operators can be overloaded as what (member, free, or both)? 回答1: The question lists three classes of operators. Putting them together on a list helps, I think, with understanding why a few operators are restricted in where they can be overloaded: Operators which have to be overloaded as members. These are

It there a need to declare const instance of a class with all attributes const?

本小妞迷上赌 提交于 2019-12-14 04:11:15
问题 This is a followup to Does a class with all attributes const need to have member function declared const as well?. So I've a class PermutationGroup whose all attribute are const. The compiler still make the distinction between const and non-const instance: struct Foo { const int bar; void meth(); }; int main() { Foo foo {2}; foo.meth(); // correct const Foo cfoo {1}; cfoo.meth(); // wrong }; As noticed by @nosid in the referred question One cannot call a non const member function a const

ref-qualified member functions as template arguments?

筅森魡賤 提交于 2019-12-14 00:20:05
问题 This compiles fine in clang 3.3: template <typename T> struct M; template <typename R, typename C, typename... A> struct M <R (C::*)(A...)> { }; template <typename R, typename C, typename... A> struct M <R (C::*)(A...) &> { }; but fails in gcc 4.8.1: [...] error: redefinition of ‘struct M <R (C::*)(A ...)>’ struct M <R (C::*)(A...) &> { }; ^ [...] error: previous definition of ‘struct M <R (C::*)(A ...)>’ struct M <R (C::*)(A...)> { }; ^ When used in different contexts, this results in all

C++: Store pointer to a member function of an object in another object

允我心安 提交于 2019-12-13 21:18:07
问题 I have a class which shall invoke a function specified by the user on certain occasions. Therefore the class has a method void setExternalPostPaintFunction(void(*function)(QPainter&)); that can be used to "register" a function. This function then will be called on that occasion: class A { public: void setExternalPostPaintFunction(void(*function)(QPainter&)); private: void (*_externalPostPaint)(QPainter&); bool _externalPostPaintFunctionAssigned; }; The function pointer is saved in the member

Where to put a member function template

杀马特。学长 韩版系。学妹 提交于 2019-12-12 11:35:20
问题 An aspect of C++ that periodically frustrates me is deciding where templates fit between header files (traditionally describing the interface) and implemention (.cpp) files. Templates often need to go in the header, exposing the implementation and sometimes pulling in extra headers which previously only needed to be included in the .cpp file. I encountered this problem yet again recently, and a simplified example of it is shown below. #include <iostream> // for ~Counter() and countAndPrint()

c++ passing class method as argument to a class method with templates

无人久伴 提交于 2019-12-11 12:22:30
问题 I'm trying to pass a class method to another class method using template, and cannot find any answer on how to do (no C++11, boost ok): I simplified the core problem to : class Numerical_Integrator : public Generic Integrator{ template <class T> void integrate(void (T::*f)() ){ // f(); //already without calling f() i get error } } class Behavior{ void toto(){}; void evolution(){ Numerical_Integrator my_integrator; my_integrator->integrate(this->toto}; } I get as error: error: no matching

VS2013 std::function with member function [duplicate]

这一生的挚爱 提交于 2019-12-11 09:41:38
问题 This question already has an answer here : std::function not compiling in VS2012 (1 answer) Closed 5 years ago . I'm trying to use std::function with member functions like this: struct Foo { void bar(int) const { /* ... */ } }; //later on std::function<void(const Foo&, int)> fun = &Foo::bar; This works under GCC 4.8.1 but fails to compile under VS2013 with the following error: error C2664: 'void std::_Func_class<_Ret,const Foo &,int>::_Set(std::_Func_base<_Ret,const Foo &,int> *)' : cannot

mem_fn to mem_fn of member

南楼画角 提交于 2019-12-11 07:37:02
问题 This is a follow-up question to mem_fn to function of member object This is the current code. #include <vector> #include <algorithm> #include <functional> struct Int { Int(int _x = 0) : x(_x) {} int GetInt() const { return x; } int x; }; struct IntWrapper { IntWrapper(int _x = 0) : test(_x) {} int GetWrappedInt() const { return test.GetInt(); } Int test; }; template<class ContainerT, class Mem> constexpr auto maxElem(const ContainerT& _container, Mem _Pm) { auto memFn = std::mem_fn(_Pm);