pointer-to-member

Could not deduce template argument & pointer to member

元气小坏坏 提交于 2019-12-04 19:09:21
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 (ObjectType::* func)(U,U,U); }; template<class ObjectType, typename U> struct SPKSetterPattern<D,ObjectType,U

Mapping from pointers-to-member

只谈情不闲聊 提交于 2019-12-04 13:13:49
(Note: in case this feels like an X-Y problem, scroll below the separator for how I arrived at this question) I am looking for a way to store pointers-to-member-functions (of different types) and compare them for equality. I need to store a mapping from pointer-to-member-function to an arbitrary object, and then search this mapping. It doesn't have to be an associative container, a linear search is fine. Also note that the pointers serve as mapping keys only, they are never dereferenced. My current approach is this: when building the mapping, I reinterpret_cast the incoming pointer-to-member

Pointer to member that is a reference illegal?

点点圈 提交于 2019-12-03 22:36:49
Let us say I have: // This is all valid in C++11. struct Foo { int i = 42; int& j = i; }; // Let's take a pointer to the member "j". auto b = &Foo::j; // Compiler is not happy here // Note that if I tried to get a pointer to member "i", it would work, as expected. Foo f; std::cout << f.*b; // Try using the pointer to member The compiler complains that I cannot take the address of the member because it is a reference. To be precise: Semantic Issue: Cannot form a pointer-to-member to member 'j' of reference type 'int &' I know doing this seems pointless, but I am only wondering why it cannot be

Is there pointer to member traits or something like this?

廉价感情. 提交于 2019-12-03 15:58:01
Based on other my question . Consider the following code template<typename T, int N> struct A { typedef T value_type; // save T to value_type static const int size = N; // save N to size }; Look, I can use value_type and size as template parameter. typedef A<int, 2> A1; typedef A<A1::value_type, A1::size + 3> A2; // OK, A2 is A<int,5> Now I want to do the same with pointer to member: struct Foo { int m; int r; }; template<int Foo::*Mem> struct B { static int Foo::* const mp; }; template<int Foo::*Mem> int Foo::* const B<Mem>::mp = Mem; // Save pointer to member But I get error. typedef B<&Foo:

Passing pointer to 2D array c++

谁说胖子不能爱 提交于 2019-12-03 15:09:34
I'm having this problem for quite a long time - I have fixed sized 2D array as a class member. class myClass { public: void getpointeM(...??????...); double * retpointM(); private: double M[3][3]; }; int main() { myClass moo; double *A[3][3]; moo.getpointM( A ); ??? A = moo.retpointM(); ??? } I'd like to pass pointer to M matrix outside. It's probably very simple, but I just can't find the proper combination of & and * etc. Thanks for help. double *A[3][3]; is a 2-dimensional array of double * s. You want double (*A)[3][3]; . Then, note that A and *A and **A all have the same address, just

How to save pointer to member in compile time?

ぃ、小莉子 提交于 2019-12-03 15:03:52
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 WORK! static int Foo::* const saved_ptr_to_member = ptr_to_member; }; // Example of using int main() {

Why can't I downcast pointer to members in template arguments?

廉价感情. 提交于 2019-12-01 18:33:23
问题 If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo { }; template<class T, int T::* z> struct Buzz { }; static int Bar::* const workaround = &Foo::x; int main() { // This works. Downcasting of pointer to members in

Why can't I downcast pointer to members in template arguments?

こ雲淡風輕ζ 提交于 2019-12-01 18:03:09
If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo { }; template<class T, int T::* z> struct Buzz { }; static int Bar::* const workaround = &Foo::x; int main() { // This works. Downcasting of pointer to members in general is fine. int Bar::* y = &Foo::x; // But this doesn't, at least in G++ 4.2 or Sun C++ 5.9. Why

Overloaded member function pointer to template

那年仲夏 提交于 2019-11-30 14:14:12
问题 I'm trying to store member function pointers by templates like this: (This is a simplified version of my real code) template<class Arg1> void connect(void (T::*f)(Arg1)) { //Do some stuff } template<class Arg1> void connect(void (T::*f)()) { //Do some stuff } class GApp { public: void foo() {} void foo(double d) {} }; Then I want to do like the following for every overloaded methods in GApp: connect(&GApp::foo); Calling this for foo() is ok, but how can I call this for foo(double d) ? Why isn

Overloaded member function pointer to template

柔情痞子 提交于 2019-11-30 09:59:08
I'm trying to store member function pointers by templates like this: (This is a simplified version of my real code) template<class Arg1> void connect(void (T::*f)(Arg1)) { //Do some stuff } template<class Arg1> void connect(void (T::*f)()) { //Do some stuff } class GApp { public: void foo() {} void foo(double d) {} }; Then I want to do like the following for every overloaded methods in GApp: connect(&GApp::foo); Calling this for foo() is ok, but how can I call this for foo(double d) ? Why isn't the following working? connect((&GApp::foo)(double)); It will give me syntax error : 'double' should