I tried to create a function template that would call a member function in an object, specified by the template parameter, but I failed. Now I have multiple functions that differ only very slightly:
static void resize_callback(raw_resource* ptr, int width, int height)
{
owner* instance = static_cast<owner*>(get_owner(ptr));
assert(instance != nullptr);
instance->on_resize(width, height);
}
static void focus_callback(raw_resource* ptr, bool state)
{
owner* instance = static_cast<owner*>(get_owner(ptr));
assert(instance != nullptr);
instance->on_focus(state);
}
static void other_callback(raw_resource* ptr, type1 arg1, type2 arg2, type3 arg3)
{
owner* instance = static_cast<owner*>(get_owner(ptr));
assert(instance != nullptr);
instance->on_other(arg1, arg2, arg3);
}
...
Each raw_resource has exactly one owner, where owner is a wrapper around raw_resource. The callbacks are fed to a low level C library that doesn't know about owners, but only about raw_resources, so they must take a raw_resource*, that a pointer to them (callbacks) has the appropriate type. The operations in the callbacks need to use the owner objects however, so they retrieve them through get_owner().
How could I make a general function template out of these that would avoid code duplication? I need instantiations of this template to be convertible to appropriate function pointers that will be compatible with their current signatures, so they can be given to the low level C library.
You could use a parameter pack:
template <typename Func, typename... Types>
static void callback(raw_resource* ptr, Func func, Types... args)
{
owner* instance = static_cast<window*>(get_owner(ptr));
assert(instance != nullptr);
(instance->*func)(args...);
}
You might want to pass Types differently.
Used like:
callback(someptr, &owner::on_resize, width, height);
If you have C++17 available you could use std::invoke instead of the ->* syntax.
As the number of arguments in each member function call is different then you can use the following approach
#include <iostream>
struct A
{
static void swap( int *x, int *y )
{
int tmp = *x;
*x = *y;
*y = tmp;
}
void set( bool b )
{
this->b = b;
}
bool b;
};
template <typename ...Args>
void operation( A *ptr, Args ... args )
{
if constexpr( sizeof...( args ) == 1 )
{
ptr->set( args... );
}
else if constexpr( sizeof...( args ) == 2 )
{
ptr->swap( args... );
}
}
int main()
{
A a = { false };
operation( &a, true );
std::cout << "a.b = " << a.b << '\n';
int x = 10; int y = 20;
std::cout << "x = " << x << ", y = " << y << '\n';
operation( &a, &x, &y );
std::cout << "x = " << x << ", y = " << y << '\n';
}
The program output is
a.b = 1
x = 10, y = 20
x = 20, y = 10
Related
I have a class
class MyClass{
public:
int var_x;
int var_y;
int var_z;
}
Then a file.txt:
var_y
var_z
In the main I want to be able to get the address of each variable in file.txt:
int main()
{
MyClass *obj;
obj = new MyClass();
std::vector<std::string>> varList = readFile("file.txt");
// I need generic code that would do the following:
// But the class can have any number (or named) variables
// And the text file can have any subset of the variables
std::cout << "var_y " << &(obj->var_y) << std::endl;
std::cout << "var_y " << &(obj->var_z) << std::endl;
delete obj;
return 0;
// Pseudo code would be
for var in varList:
addr = get_addr_from_string(var)
}
using Gettor = std::function< std::any(void*) > get_ptr;
template<class T>
struct ReflectedClass {
static std::unordered_map<std::string, Gettor> members;
template<class U>
static U* get( T* t, const char* name ) {
auto it = members.find(name);
if (it == members.end()) return nullptr;
std::any a = it->second( t );
U** ptr = std::any_cast<U*>(&a);
if (!ptr) return nullptr;
return *ptr;
}
};
#define TO_STR2(X) ##X
#define TO_STR(X) TO_STR2(X)
#define REFLECTED_MEMBER( CLASS, NAME ) \
ReflectedClass<CLASS>::members[ TO_STR(NAME) ] = \
[]( void* ptr )->std::any { \
return std::addressof( (CLASS*)(ptr)->NAME ); \
}
Then simply do
REFLECTED_MEMBER(MyClass, var_x )
REFLECTED_MEMBER(MyClass, var_y )
REFLECTED_MEMBER(MyClass, var_z )
and you can call
MyClass foo;
int* ptr = ReflectedClass<MyClass>::get<int>( &foo, "var_x" );
and it returns a pointer to foo.var_x.
This is a bad idea, don't do it.
I was trying to make a function that assigns y to x regardless whether x, y are int or std::string. I wrote this code:
#include <iostream>
#include <string>
#include <typeinfo>
template <typename T>
T& assign(T& x, T& y){
if ( typeid(x).name() == "Ss" && typeid(y).name() == "Ss" ){
std::string k = static_cast<std::string>(y);
x = k;
return x;
}
else if ( typeid(x).name() == "i" && typeid(y).name() == "i" ){
int k = static_cast<int>(y);
x = k;
return x;
}
else{
std::cout << "uncorrect assignment" << std::endl;
}
}
int main(){
std::string a = "empty_string";
std::string b = "Hi there";
assign(a, b);
std::cout << a << std::endl;
}
But it doesn’t work.
It gives the error:
[Error] invalid static_cast from type ‘std::basic_string<char>’ to type
at line 14:
int k = static_cast<int>(y);
I can’t understand, what is the problem?
I know the objection: I might have just defined function assign as:
template <typename T>
T& assign(T& x, T& y){
x = y;
}
which works. However, I was working on an other more complex function on which I have to (or at least I haven’t found any way other than) use static_cast.
So, if you could, please, explain to me what is the mistake in this example, I may try to fix the function I am working on.
Thank you very much,
Simone.
To do what do you want, you need C++17 and if constexpr. And the use of something that works compile-time, not of typeid that works runtime.
The problem is that with your code, typeid permit, runtime, to choose the if or the else part of your code, but the compiler must compile both part. So must compile
int k = static_cast<int>(y);
x = k;
when T is std::string. This give an error.
You need a type-traits (std::is_same, by example), that is evaluated compile-time, and a construct that avoid the compilation of the wrong part. This construct is if constexpr ( <test> ) (where the <test> is valuable compile time) but, unfortunately, is available only from C++17.
So, in C++17 you can write
template <typename T>
void assign (T & x, T const & y)
{
if constexpr ( std::is_same<T, std::string>::value ) {
std::string k = static_cast<std::string>(y);
x = k;
}
else if constexpr ( std::is_same<T, int>::value ) {
int k = static_cast<int>(y);
x = k;
}
else {
std::cout << "uncorrect assignment" << std::endl;
}
}
but, pre C++17, you have to follows different ways.
To handle different types separately inside a function, an option is to define a local struct with overloaded function call operators to different types:
#include <iostream>
#include <string>
template<typename T>
T& assign(T& x, const T& y) {
struct {
void operator()(std::string& lhs, const std::string& rhs) {
std::cout << "Type is std::string." << std::endl;
lhs = rhs;
}
void operator()(int& lhs, const int& rhs) {
std::cout << "Type is int." << std::endl;
lhs = rhs;
}
} assign_impl;
assign_impl(x, y);
return x;
}
int main() {
/* Test No. 1 */ {
std::string dest, src = "Foo";
std::cout << "Test result: " << assign(dest, src) << std::endl;
}
/* Test No. 2 */ {
int dest, src = 32;
std::cout << "Test result: " << assign(dest, src) << std::endl;
}
}
The code above will work on C++98 and above but its disadvantage is that it will raise compiler errors if you try to use it with unhandled types.
As continue of thread Compare of std::function with lambda
I have problem with capture this to lambda - and compare after that with "correct callback".
#include <iostream>
#include <functional>
using namespace std;
//////////////////////////////////////////////////////////////////////////
class IFoo
{
public:
virtual void onAppStarted( int, int ) = 0;
};
//////////////////////////////////////////////////////////////////////////
template <typename T>
struct FunctionTypeDeduction;
template<class ClassType, class ReturnType, class... Args>
struct FunctionTypeDeduction< ReturnType( ClassType::* )( Args... ) >
{
using type = ReturnType( *)( Args... );
using functorType = std::function<ReturnType( Args... ) >;
};
//////////////////////////////////////////////////////////////////////////
class SubcribeSystem
{
public:
using tSomeMethodType = FunctionTypeDeduction< decltype( &IFoo::onAppStarted ) >::type;
using tSomeMethodCallback = FunctionTypeDeduction< decltype( &IFoo::onAppStarted ) >::functorType;
public:
void subribeOnSomeMethod( const tSomeMethodCallback& arr )
{
arr( 3, 19 );
}
};
#define SUBCRIBE_FOR_SOMEMETHOD( eventsObj, lambda ) \
do \
{ \
SubcribeSystem::tSomeMethodType const func_ptr = lambda; \
eventsObj.subribeOnSomeMethod( lambda ); \
} \
while(false);
//////////////////////////////////////////////////////////////////////////
class Bar
{
public:
void fn( SubcribeSystem& events )
{
auto onAppStart = []( int width, int height )
{
cout << width << " " << height << endl;
};
auto onAppStart2 = []( bool width, int height )
{
cout << width << " " << height << endl;
};
auto onAppStart3 = [this]( int width, int height )
{
cout << width << " " << height << endl;
someOtherMethod( width );
};
SUBCRIBE_FOR_SOMEMETHOD( events, onAppStart ); // expect all ok
//SUBCRIBE_FOR_SOMEMETHOD( events, onAppStart2 ); // expect error cause bool first param
SUBCRIBE_FOR_SOMEMETHOD( events, onAppStart3 ); // expect all ok, but not !!!
}
void someOtherMethod( int x )
{
cout << "processed callback " << x << endl;
}
};
int main()
{
Bar bar;
SubcribeSystem sub;
bar.fn( sub );
}
In your macro you have
SubcribeSystem::tSomeMethodType const func_ptr = lambda;
Where SubcribeSystem::tSomeMethodType is a
FunctionTypeDeduction< decltype( &IFoo::onAppStarted ) >::type
which is a function pointer. So you are trying to convert the lambda into a function pointer. Unfortunately a lambda that captures, which onAppStart3 does, cannot be converted to a function pointer.
The first two lambdas work as they do not capture so they have an implicit function pointer conversion operator.
In my callback system I want to store std::function (or something else) with varying arguments.
Example:
I want to call void()
I want to call void(int, int)
I want 1) and 2) to be stored in the same variable and choose what to call in actuall call
FunctionPointer f0;
FunctionPointer f2;
f0();
f2(4, 5);
Is it possible to do something like this? Or I have to create several "FuntionPointer" templates based on input arguments count.
EDIT
Is it possible to utilize std::bind somehow for this task? With std::bind I can have std::function<void()> f = std::bind(test, 2, 5);
EDIT 2
Practical use case: I have a trigger system and I want to assign funtion pointers to actions, so when action happen, function is called.
Pseudo-code sample:
structure Trigger
{
Function f;
}
Init:
Trigger0.f = pointer to some function ()
Trigger1.f = pointer to some function (a, b)
Input:
Find Trigger by input
if (in == A) Trigger.f();
else Trigger.f(10, 20)
or if possible
Input:
Find Trigger by input
if (in == A) f = bind(Trigger.f);
else f = bind(Trigger.f, 10, 20);
f()
std::function<void()> and std::function<void(int, int)> are two absolutely distinct types. You need some sort of union functionality (or polymorphism) to store an object of an unknown type.
If you can use Boost, you could easily do this with boost::variant:
// Declaration:
boost::variant<std::function<void()>, std::function<void(int, int)> > f;
// Calling, explicit:
if (fContainsNullary()) {
boost::get<std::function<void()>>(f)();
} else {
boost::get<std::function<void(int, int)>>(f)(4, 5);
}
It is up to you to provide the logic of fContainsNullary(). Alternatively, you can use the variant's own stored knowledge of value type by using a visitor:
struct MyVisitor : boost::static_visitor<void>
{
result_type operator() (const std::function<void()> &a) {
a();
}
result_type operator() (const std::function<void(int, int)> &a) {
a(4, 5);
}
};
// Calling, via visitor:
boost::apply_visitor(MyVisitor(), f);
If Boost is not an option, you can hand-craft a suitable union for much the same purpose.
The following solution might work for you (I'm not sure that the code is absolutely correct here):
Create a wrapper for std::function with virtual destructor to enable using dynamic cast
class function_wrapper_base
{
virtual ~function_wrapper_base();
}
template <class... Args>
class function_wrapper
: public function_wrapper_base
{
public:
std::function<void, Args...> f;
...
};
Then create a class variant_function_holder
class variant_function_holder
{
std::unique_ptr<function_wrapper_base> f;
...
template <class... Args>
void operator()(Args&&... args)
{
function_wrapper<std::decay<Args>::type...> * g = dynamic_cast<function_wrapper<std::decay<Args>::type...>>(f.get());
if (g == nullptr)
{
// ToDo
}
g->f(std::forward<Args>(args)...);
}
};
Well, if you can use RTTI, you can define a MultiFuncObject like this, and you can easily bind other functions. Also, you can easily call them. But unfortunately, this approach only works for a limited number of arguments. But actually boost::bind also supports limited number of arguments (by default 9). So you can extend this class to satisfy your needs.
Before giving you the source of MultiFuncObject, I want to show you how you can use it. It takes an template argument to be used as return type. You can bind new functions with += operator. With some template magic, the class distinguishes differences between bound functions with same count of arguments with at least one different argument type.
You need C++11, because MultiFuncObject uses std::unordered_map and std::type_index.
Here is usage:
#include <iostream>
using namespace std;
void _1() {
cout << "_1" << endl;
}
void _2(char x) {
cout << "_2" << " " << x << endl;
}
void _3(int x) {
cout << "_3" << " " << x << endl;
}
void _4(double x) {
cout << "_4" << " " << x << endl;
}
void _5(int a, int b) {
cout << "_5" << " " << a << " " << b << endl;
}
void _6(char a, int b) {
cout << "_6" << " " << a << " " << b << endl;
}
void _7(int a, int b, int c) {
cout << "_7" << " " << a << " " << b << " " << c << endl;
}
int main() {
MultiFuncObject<void> funcs;
funcs += &_1;
funcs += &_2;
funcs += &_3;
funcs += &_4;
funcs += &_5;
funcs += &_6;
funcs += &_7;
funcs();
funcs('a');
funcs(56);
funcs(5.5);
funcs(2, 5);
funcs('q', 6);
funcs(1, 2, 3);
return 0;
}
I hope this is close to what you want. Here is the source of MultiFuncObject:
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
using namespace std;
template <typename R>
class MultiFuncObject {
unordered_map<type_index, void (*)()> m_funcs;
public:
MultiFuncObject<R> operator +=( R (* f)() ) {
m_funcs[typeid( R() )] = (void (*)()) f;
return *this;
}
template <typename A1>
MultiFuncObject<R> operator +=( R (* f)(A1) ) {
m_funcs[typeid( R(A1) )] = (void (*)()) f;
return *this;
}
template <typename A1, typename A2>
MultiFuncObject<R> operator +=( R (* f)(A1, A2) ) {
m_funcs[typeid( R(A1, A2) )] = (void (*)()) f;
return *this;
}
template <typename A1, typename A2, typename A3>
MultiFuncObject<R> operator +=( R (* f)(A1, A2, A3) ) {
m_funcs[typeid( R(A1, A2, A3) )] = (void (*)()) f;
return *this;
}
R operator()() const
{
unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid( R() ));
if (it != m_funcs.end()) {
R (*f)() = ( R (*)() )(it->second);
(*f)();
}
}
template <typename A1>
R operator()(A1 a1) const
{
unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid( R(A1) ));
if (it != m_funcs.end()) {
R (*f)(A1) = ( R (*)(A1) )(it->second);
(*f)(a1);
}
}
template <typename A1, typename A2>
R operator()(A1 a1, A2 a2) const
{
unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid( R(A1, A2) ));
if (it != m_funcs.end()) {
R (*f)(A1, A2) = ( R (*)(A1, A2) )(it->second);
(*f)(a1, a2);
}
}
template <typename A1, typename A2, typename A3>
R operator()(A1 a1, A2 a2, A3 a3) const
{
unordered_map<type_index, void (*)()>::const_iterator it = m_funcs.find(typeid( R(A1, A2, A3) ));
if (it != m_funcs.end()) {
R (*f)(A1, A2, A3) = ( R (*)(A1, A2, A3) )(it->second);
(*f)(a1, a2, a3);
}
}
};
It stores different function prototypes using std::unordered_map with keys of std::type_index and values of void (*)(). When needed, the correct function is retrieved using that map.
Here is the working example
C++11 to the rescue!
If you can generalize your function to a functor object taking no arguments, then you can call it with any lambda.
#include <iostream>
using namespace std;
template <class F>
void call_it(F&& f)
{
f();
}
int main()
{
int x = 50, y = 75;
call_it([] () { cout << "Hello!\n"; });
call_it([x,y] () { cout << x << " + " << y << " = " << x + y << '\n';});
return 0;
}
If std::function is not necessary for you, you can create a proxy class.
class fn_t {
public:
typedef void (*fn_1_t)();
typedef void (*fn_2_t)(int, int);
fn_1_t fn_1;
fn_2_t fn_2;
fn_t operator=(fn_1_t func_1) { fn_1 = func_1; return *this; }
fn_t operator=(fn_2_t func_2) { fn_2 = func_2; return *this; }
void operator()() { (*fn_1)(); }
void operator()(int a, int b) { (*fn_2)(a, b); }
};
#include <iostream>
using namespace std;
void first() {
cout << "first" << endl;
}
void second(int a, int b) {
cout << "second " << a << " : " << b << endl;
}
int main() {
fn_t f;
f = &first;
f = &second;
f();
f(5, 4);
return 0;
}
Class fn_t automatically works with two prototypes you want, assigns automatically needed one, and it can call functions with both prototypes by overlading () operator with appropriate parameters.
You may want to check for validity of function pointers fn_1 and fn_2 but I didn't include this checking for minimality.
The advantage of this is that you only need C++ and not even STL and Boost.
The other answers are fine but I want to show my solution as well.
It's a small header with which you can "elongate" function signatures.
This allows you to do this (extract from the github example):
int foo_1p(int a);
int foo_2p(int a, int b);
int foo_3p(int a, int b, int c);
int foo_4p(int a, int b, int c, int d);
int foo_5p(int a, int b, int c, int d, int e);
int foo_6p(int a, int b, int c, int d, int e, int f);
int foo_7p(int a, int b, int c, int d, int e, int f, std::string g);
...
int main()
{
std::unordered_map<std::string, std::function<int(int, int, int, int, int, int, std::string)>> map;
map["foo_1p"] = ex::bind(foo_1p, ph, ph, ph, ph, ph, ph);
map["foo_2p"] = ex::bind(foo_2p, ph, ph, ph, ph, ph);
map["foo_3p"] = ex::bind(foo_3p, ph, ph, ph, ph);
map["foo_4p"] = ex::bind(foo_4p, ph, ph, ph);
map["foo_5p"] = ex::bind(foo_5p, ph, ph);
map["foo_6p"] = ex::bind(foo_6p, ph);
map["foo_7p"] = foo_7p;
for (const auto& f : map)
{
std::cout << f.first << " = " << f.second(1, 1, 1, 1, 1, 1, "101") << std::endl;
}
}
How to pass optional arguments to a method in C++ ?
Any code snippet...
Here is an example of passing mode as optional parameter
void myfunc(int blah, int mode = 0)
{
if (mode == 0)
do_something();
else
do_something_else();
}
you can call myfunc in both ways and both are valid
myfunc(10); // Mode will be set to default 0
myfunc(10, 1); // Mode will be set to 1
An important rule with respect to default parameter usage:
Default parameters should be specified at right most end, once you specify a default value parameter you cannot specify non default parameter again.
ex:
int DoSomething(int x, int y = 10, int z) -----------> Not Allowed
int DoSomething(int x, int z, int y = 10) -----------> Allowed
It might be interesting to some of you that in case of multiple default parameters:
void printValues(int x=10, int y=20, int z=30)
{
std::cout << "Values: " << x << " " << y << " " << z << '\n';
}
Given the following function calls:
printValues(1, 2, 3);
printValues(1, 2);
printValues(1);
printValues();
The following output is produced:
Values: 1 2 3
Values: 1 2 30
Values: 1 20 30
Values: 10 20 30
Reference: http://www.learncpp.com/cpp-tutorial/77-default-parameters/
To follow the example given here, but to clarify syntax with the use of header files, the function forward declaration contains the optional parameter default value.
myfile.h
void myfunc(int blah, int mode = 0);
myfile.cpp
void myfunc(int blah, int mode) /* mode = 0 */
{
if (mode == 0)
do_something();
else
do_something_else();
}
With the introduction of std::optional in C++17 you can pass optional arguments:
#include <iostream>
#include <string>
#include <optional>
void myfunc(const std::string& id, const std::optional<std::string>& param = std::nullopt)
{
std::cout << "id=" << id << ", param=";
if (param)
std::cout << *param << std::endl;
else
std::cout << "<parameter not set>" << std::endl;
}
int main()
{
myfunc("first");
myfunc("second" , "something");
}
Output:
id=first param=<parameter not set>
id=second param=something
See https://en.cppreference.com/w/cpp/utility/optional
Use default parameters
template <typename T>
void func(T a, T b = T()) {
std::cout << a << b;
}
int main()
{
func(1,4); // a = 1, b = 4
func(1); // a = 1, b = 0
std::string x = "Hello";
std::string y = "World";
func(x,y); // a = "Hello", b ="World"
func(x); // a = "Hello", b = ""
}
Note : The following are ill-formed
template <typename T>
void func(T a = T(), T b )
template <typename T>
void func(T a, T b = a )
With commas separating them, just like parameters without default values.
int func( int x = 0, int y = 0 );
func(); // doesn't pass optional parameters, defaults are used, x = 0 and y = 0
func(1, 2); // provides optional parameters, x = 1 and y = 2
Typically by setting a default value for a parameter:
int func(int a, int b = -1) {
std::cout << "a = " << a;
if (b != -1)
std::cout << ", b = " << b;
std::cout << "\n";
}
int main() {
func(1, 2); // prints "a=1, b=2\n"
func(3); // prints "a=3\n"
return 0;
}
Jus adding to accepted ans of #Pramendra , If you have declaration and definition of function, only in declaration the default param need to be specified