🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Pointer to member function with default variable value

Started by
8 comments, last by taby 4 years, 2 months ago

I'm trying to call a member function that has a default variable value, ie: void func(size_t x = 1234) { }

Is there any way to omit the function parameter when called via a pointer to member function?

#include <iostream>
using namespace std;


class c
{
public:
	int f(size_t x = 1234)
	{
		cout << x << endl;
		return 1;
	}

	bool b;
};

int main(void)
{
	typedef  int (c::* c_member_function)(size_t x);

	c_member_function ptr = &c::f;

	c c_instance;

	(c_instance.*ptr)(1);

	return 0;
}

Advertisement

It doesn't seem to work if you want to call the function with the default parameter but without defining it explicitely in your call. I guess that a default value is just some compile time convenience and will let the compiler fill in the parameter for you. So instead of

f();

the compiler intreprets your code as

f(1234); //found default parameter; add 1234 for parameter[0] x

You don't do default parameters. Make another function.

Found here some explanations https://quuxplusone.github.io/blog/2020/04/18/default-function-arguments-are-the-devil/

keep your “ifs” on top of the stack

If you're asking if you can forward the default parameter value to the pointer-to-member call site, the answer is no.

You are better off using a lambda as a forwarding function. No need to faff with pointer-to-member-functions and risk having badly-written web sites turn your code into emojis. Embrace the modern solution of lambdas and std::function, eschew the weird and cryptic olde-timey solutions that came out of the first wave of C-with-classes. You will not regret it.

Stephen M. Webb
Professional Free Software Developer

Interesting! Thanks so much for everyone's input.

C++ is so powerful:

#include <iostream>
using namespace std;

class c;
typedef int (c::* c_member_function_pointer)(size_t x);

class c
{
public:		
	int f(size_t x = 1)
	{
		cout << x << endl;
		return 1;
	}

	c_member_function_pointer ptr = &c::f;

	void proceed(void)
	{
		(this->*(this->ptr))(12345);
	}

	bool b;
};

int main(void)
{
	c c_instance;
	c_instance.proceed();

	return 0;
}

taby said:
C++ is so powerful:

Ummm…..

Dang, I think my eyes are bleeding.

Stephen M. Webb
Professional Free Software Developer

It's always dangerous to “have fun with functions pointers” in production code. This… “fun” will only take you in four directions:

  • eye bleeding fragile pointers that only you (only now) understand
  • template constructs (type correct contamination), best runtime performance, slowly verifiable by compiler. less eye bleeding
  • an interface with humanly named methods
  • std::function or something similar (type correctnes, type erasure) called “a delegate”. nice, almost no eye bleeding - much less than with plain C function pointers. possibly the easiest one to use

Based on these you can create wrappers - callable object calling another callable object using predefined parameter (don't use std::bind, lambda is the way to go). A function pointer can be sometimes though of as callable object. It's getting too complicated for simple explanation…

keep your “ifs” on top of the stack

The deed is done LOL: https://github.com/sjhalayka/julia4d3

I went from a multi-threaded to a single-threaded design, that employs a gigantic state machine. It runs just as fast as the multi-threaded version, although the interface is polled only every 333 milliseconds during mesh generation.

I used the function pointers because they sure beat doing a switch statement or if-else if-else blocks.

The next app I do will go back to multi-threaded, but this next time I'll be using GLFW and imgui.

This topic is closed to new replies.

Advertisement