member-functions

How to tell if class contains a certain member function in compile time [duplicate]

做~自己de王妃 提交于 2019-12-30 07:01:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? say there are 2 classes: struct A{ int GetInt(){ return 10; } }; struct B{ int m; }; I want to use object of type A or B in following function tempate< typename T > int GetInt( const T & t ) { //if it's A, I'll call: return t.GetInt(); //if its' B, I'll call: return t.m; } Now, because there are whole bunch of classes, some contain

Generate Unique Identifier that can distinguish ID(Foo::a()) from ID(Foo::b())

别等时光非礼了梦想. 提交于 2019-12-24 17:43:41
问题 Say I have: struct S{ void f(int); float g(int,int); void h(int); } #define UID(w) /* how to do it? */ cout << UID(S::f); cout << UID(S::g); cout << UID(S::h); I need some way of creating a unique number, string or address for each member. This is because I'm going to be using: #define BIND(foo) Generate<decltype(&foo), &foo>::call u = & BIND(S::f) v = & BIND(S::g) w = & BIND(S::h) i.e. BIND generates an associated C-style function Here is a sketch of the generator: template< typename F f >

C++ pthread member function [duplicate]

为君一笑 提交于 2019-12-24 10:24:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: pthread Function from a Class I have this code that I can't get to compile because of the pthread_create line: void* gtk_functor::_threaded_run(void* win) { Gtk::Window* w = static_cast<Gtk::Window*>(win); Gtk::Main::run(*w); delete w; } void gtk_functor::operator ()(Gtk::Window& win, bool threaded) { if (threaded) { pthread_t t_num; pthread_create(&t_num, NULL, (void* (*)(void*))&gtk_functor::_threaded_run,

How to get Windows thread pool to call class member function?

☆樱花仙子☆ 提交于 2019-12-24 00:47:19
问题 I want the Windows thread pool (QueueUserWorkItem()) to call my class' member functions. Unfortunately this cannot be done directly by passing a member function pointer as an argument to QueueUserWorkItem(). What makes it difficult is that more than one member function must be callable and they have different signatures (all return void though). One probably need to add a few layers of abstraction to get this to work, but I'm not sure how to approach this. Any ideas? 回答1: This might help. You

How to get Windows thread pool to call class member function?

☆樱花仙子☆ 提交于 2019-12-24 00:42:48
问题 I want the Windows thread pool (QueueUserWorkItem()) to call my class' member functions. Unfortunately this cannot be done directly by passing a member function pointer as an argument to QueueUserWorkItem(). What makes it difficult is that more than one member function must be callable and they have different signatures (all return void though). One probably need to add a few layers of abstraction to get this to work, but I'm not sure how to approach this. Any ideas? 回答1: This might help. You

Template member functions with trailing return type, giving errors even if unused

China☆狼群 提交于 2019-12-21 04:49:20
问题 I understand that template member functions are only generated if used. This is convenient if not all used types support such a function. However, this does not appear to work for functions with trailing return type specification. Below is a small experiment: // helper function for case A workaround template <typename A, typename T> auto F(T&& x) -> decltype(x.template f <A>()) { return x.template f <A>(); } // helper function for case B workaround template <typename A, typename T> auto G(T&&

Undefined reference when inline specifier used with class member

对着背影说爱祢 提交于 2019-12-20 03:55:15
问题 I have some member functions in a class. When I use the inline specifier, the compiler complains of undefined reference. I have tried: Using 'inline' to precede the function definition in the class header file only. Using 'inline' to precede the function declaration in the class .cpp (where the member functions are specified) file only. Doing both of the above at the same time. Obviously, one of these ways is the correct thing to do, and the others are not correct. However trying each option

Free function versus member function

只谈情不闲聊 提交于 2019-12-18 18:53:42
问题 What is the advantage of having a free function (in anonymous namespace and accessible only in a single source file) and sending all variables as parameters as opposed to having a private class member function free of any parameters and accessing member variables directly? Thanks! header: Class A { int myVariable; void DoSomething() { myVariable = 1; } }; source: namespace { void DoSomething2(int &a) { a = 1; } } int A::SomeFunction() { DoSomething2(myVariable); // calling free function

Why is a public const method not called when the non-const one is private?

前提是你 提交于 2019-12-18 10:58:13
问题 Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; a.foo(); } The compiler error is: error: 'void A::foo()' is private`. But when I delete the private one it just works. Why is the public const method not called when the non-const one is private? In other words, why does overload resolution come before access control? This is strange. Do you think it is consistent? My code

Why class member functions shadow free functions with same name?

ε祈祈猫儿з 提交于 2019-12-18 05:50:08
问题 It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely I mean that every free function with the same name is not considered for overload resolution at all. I can understand why it's done with something like this: void f(); struct S { void f(); void g() { f(); // calls S::f instead of ::f } }; where the functions have identical signatures, its only natural as variable scoping works the same way. But