pointer-to-member

About shared_ptr and pointer to member operator `->*` and `std::bind`

醉酒当歌 提交于 2019-12-29 06:17:31
问题 Recently I discovered that shared_ptr does not have pointer to member operator ->* . I created simple example: template <typename Pointer, typename Function, typename... Args> auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...)) { return (p->*f)(args...); } struct A { void g() { std::cout << "A::g()\n"; } }; int main() { A a; invoke1(&a, &A::g); // works!! std::shared_ptr<A> sa = std::make_shared<A>(); invoke1(sa, &A::g); // compile error!! } Q1: Why is so? Why

Pointer-to-member as template parameter deduction

落花浮王杯 提交于 2019-12-23 21:37:30
问题 I want to get pointer-to-member as template parameter to the foo1. Here is code: struct baz{ int qux; }; template<typename C, typename T, T C::*m> struct foo1{}; template<typename C, typename T> void barr2(T C::*m){ } template<typename C, typename T> void barr1(T C::*m){ barr2(m); // ok foo1<C, T, &baz::qux> _; // ok foo1<C, T, m> f; // g++4.6.1 error here; how to pass 'm' correctly ? } int main(){ barr1(&baz::qux); } So how it should look like? 回答1: It doesn't work for you because you are

Accessing function pointer inside class

孤街醉人 提交于 2019-12-22 07:59:30
问题 I am defining function pointer inside a class and trying to access it through an instance of the class but it shows an error. Here is the code: 1 #include<stdio.h> 2 3 class pointer { 4 public: 5 int (pointer::*funcPtr)(int); 6 pointer() { 7 funcPtr = &pointer::check; 8 } 9 10 11 int check(int a) 12 { 13 return 0; 14 } 15 16 }; 17 18 int main() 19 { 20 pointer *pt=new pointer; 21 return (pt->*funcPtr)(3); 22 } It shows a compile time error: checkPointer.cpp:21:15: error: ‘funcPtr’ was not

Could not deduce template argument & pointer to member

十年热恋 提交于 2019-12-22 00:25:24
问题 I am encountering the C2783 error with Visual C++ (could not deduce template argument), I have the following test case: enum SPKType { A, B, C, D }; template<SPKType TypeCode, class ObjectType, typename U> struct SPKSetterPattern { typedef void (ObjectType::* func)(U); }; template<class ObjectType, typename U> struct SPKSetterPattern<B,ObjectType,U> { typedef void (ObjectType::* func)(U,U); }; template<class ObjectType, typename U> struct SPKSetterPattern<C,ObjectType,U> { typedef void

Does C++ support member function references?

故事扮演 提交于 2019-12-21 09:39:14
问题 C++ permits function pointers and function references. It also permits pointers-to-member-functions . But does it permit references-to-member-functions ? I can't seem to deduce the rules from the standard, and I've failed to make a program work with them. [ member function pointers ] [ member function references ] 回答1: [C++11: 8.3.3/3]: A pointer to member shall not point to a static member of a class (9.4), a member with reference type, or “ cv void.” [ Note: See also 5.3 and 5.5. The type

How to save pointer to member in compile time?

霸气de小男生 提交于 2019-12-21 04:51:54
问题 Consider the following code template<typename T, int N> struct A { typedef T value_type; // OK. save T to value_type static const int size = N; // OK. save N to size }; Look, it is possible to save any template parameter if this parameter is a typename or an integer value. The thing is that pointer to member is an offset, i.e. integer. Now I want to save any pointer to member in compile time: struct Foo { int m; int r; }; template<int Foo::*ptr_to_member> struct B { // Next statement DOES NOT

A confusing typedef involves class scope

时间秒杀一切 提交于 2019-12-20 09:09:45
问题 I'm reading code of a C++ project and it contains some code of the following form: namespace ns { class A {}; class B {}; } struct C { typedef ns::A* ns::B::* type; }; Can someone explain the meaning of the typedef line? type seems to be some kind of pointer to member of ns::B which points to ns::A , but I'm not sure. Class A and B in the real code are not empty, but I think it's not relevant here. And here is a live example. 回答1: ns::B::* is a pointer-to-member-variable of B . Then ns::A* is

Using a member function pointer within a class

China☆狼群 提交于 2019-12-18 12:33:02
问题 Given an example class: class Fred { public: Fred() { func = &Fred::fa; } void run() { int foo, bar; *func(foo,bar); } double fa(int x, int y); double fb(int x, int y); private: double (Fred::*func)(int x, int y); }; I get a compiler error at the line calling the member function through the pointer "*func(foo,bar)", saying: "term does not evaluate to a function taking 2 arguments". What am I doing wrong? 回答1: The syntax you need looks like: ((object).*(ptrToMember)) So your call would be: ((

Is it possible to use a pointer-to-member to get an actual pointer?

大憨熊 提交于 2019-12-12 06:37:20
问题 I have some struct or class example , and I need to send a pointer to any of its members somewhere. void send_ptr(void *ptr, std::size_t size); struct obj {}; struct example { int a; obj b; private: static void send_ptr(void *ptr, std::size_t size) {} public: template <typename M> void send_member(M *member) { send_ptr(member, sizeof(M)); } This works fine but I thought I could use pointer-to-member here to introduce a little static checking. (The above implementation of send_member can

gcc, can I use offsetof() with templated pointer to member?

删除回忆录丶 提交于 2019-12-11 12:33:41
问题 The code below is here: https://ideone.com/XnxAyw The compiler error I get is: prog.cpp: In member function ‘size_t list_base<T, NODE, true>::offset()’: prog.cpp:26:22: error: expected unqualified-id before ‘*’ token return offsetof(T, *NODE); ^ prog.cpp:26:22: error: expected ‘)’ before ‘*’ token Visual Studio is OK with using offsetof(type, 'pointer to member') but is that because it's lax? If so, does anyone know a standards compliant way to use offsetof() with a pointer to member template