473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function pointers and default arguments

Hi,

I'll go straight to the first question. Why does the code below
compile?

void f(int i = 0);

int main()
{
(&f)();
}

My understanding is that the type of &f is void (*)(int) and as such,
expects exactly one argument. However, the default argument of f is
used.

I tried to find out what the standard has to say but I couldn't find
anything explicitly about it (I would appreciate if someone could
point me to the correct paragraph).
The second question, which is how I got to the previous one in the
first place, is how can I "disable" the default arguments of a
function. The reason for this is that I have to call a (large) number
of functions with (lots of) default arguments based on values from an
XML like structure. It's easy enough to read all the arguments from
the XML and directly call the required function but I want the code to
break if someone adds an extra default argument. This way people will
be reminded to ammend the XML code to cope with the extra argument.

The only easy solution I found so far is by using BOOST_TYPEOF.
Something like:

void FunctionWithLot sOfArgs(int, int = 0, double = 1.0, etc);

// read params p1, p2,... from XmL
BOOST_TYPEOF(&F unctionWithLots OfArgs) fcn = &FunctionWithLo tsOfArgs;
fcn(p1, p2,...);

However, there is a problem with this solution. It doesn't work on our
current compiler. We are in the process of migrating to a new version
but it may take a few months before this is completed.

In my opinion, the obvious

void (*FPtr)(int, int, ...) = &FunctionWithLo tsOfArgs;
FPtr(p1, p2, ...);

is verbouse enough to put people off. But it works on our compiler.

So, anyone knows any nifty tricks for disabling the default arguments?

Regards,
Claudiu

P.S. Another solution I tried that fails on our compiler (it works on
others) is

template<typena me T>
class FuncPtr
{
public:
FuncPtr(const T& ptr) : ptr_(ptr_){}
const T& operator()() const { return ptr_; }
private:
T ptr_;
};

template<typena me T>
FuncPtr<TMakeFu ncPtr(const T& ptr)
{
return FuncPtr<T>(ptr) ;
}

void f(int i = 0);

int main()
{
MakeFuncPtr(&f) ()();
}

Aug 4 '07 #1
12 7820
claudiu wrote:
Hi,

I'll go straight to the first question. Why does the code below
compile?

void f(int i = 0);

int main()
{
(&f)();
}

My understanding is that the type of &f is void (*)(int) and as such,
expects exactly one argument. However, the default argument of f is
used.
Default parameters are part of the type.

....
>
The second question, which is how I got to the previous one in the
first place, is how can I "disable" the default arguments of a
function. ...

The code below does somthing like what you're looking at doing.
-----
// This F() takes function pointers of functions with no params
template <typename Tr>
Tr (* F( Tr (*fp)( ) ) )( )
{
return fp;
}

// This F(fp) takes function pointers of functions with 1 param
template <typename Tr, typename T1>
Tr (* F( Tr (*fp)( T1 ) ) )( T1 )
{
return fp;
}

// This F(fp) takes function pointers of functions with 2 params
template <typename Tr, typename T1, typename T2>
Tr (* F( Tr (*fp)( T1, T2 ) ) )( T1, T2 )
{
return fp;
}

// F(fp) may be extended to as many parameters you may want to handle
// test code
int Z( int i = 0)
{
return i;
}

int X( int i = 0, double d = 4.4)
{
return i;
}

int main()
{
Z();
X();

// works
F( Z )(5);
F( X )( 1, 1.1 );

// fails
//F( Z )();
//F( X )( 1 );
}
-----

This defines a number of overloaded F template functions that take
pointers to functions of different numbers of parameters and return the
pointer. The parameters do not define a "default" so the type of the
functions returned do not have default parameters but are otherwise the
same as the function passed in.
Aug 4 '07 #2
On Aug 5, 12:23 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
claudiu wrote:
I'll go straight to the first question. Why does the code below
compile?
void f(int i = 0);
int main()
{
(&f)();
}
My understanding is that the type of &f is void (*)(int) and as such,
expects exactly one argument. However, the default argument of f is
used.
Default parameters are part of the type.
No they're not. The type of the function f, above, is void
(int). Try it:
void (*pf)( int ) = &f ;
or even:
void (*pf)( int i = 42 ) = &f ;

The reason the default arguments are used in his case is because
the expression invoking the function has default arguments
associated with it.

--
James Kanze (GABI Software) email:james.kan ze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 4 '07 #3
James Kanze wrote:
On Aug 5, 12:23 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
>claudiu wrote:
>>I'll go straight to the first question. Why does the code below
compile?
>>void f(int i = 0);
>>int main()
{
(&f)();
}
>>My understanding is that the type of &f is void (*)(int) and as such,
expects exactly one argument. However, the default argument of f is
used.
>Default parameters are part of the type.

No they're not. The type of the function f, above, is void
(int). Try it:
void (*pf)( int ) = &f ;
or even:
void (*pf)( int i = 42 ) = &f ;

The reason the default arguments are used in his case is because
the expression invoking the function has default arguments
associated with it.
Interesting; this code compiles in gcc but not on comeau or VC++ 2005.

int f( int p = 5 )
{
return p;
}

template <typename T>
T F( T fp )
{
return fp;
}

#include <iostream>

int main()
{
std::cout << F( f )() << "\n";
}

Ok, so who is wrong, GCC or Comeau + VC++ ?
Aug 5 '07 #4
On Aug 4, 5:13 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
James Kanze wrote:
On Aug 5, 12:23 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
claudiu wrote:
I'll go straight to the first question. Why does the code below
compile?
>void f(int i = 0);
>int main()
{
(&f)();
}
>My understanding is that the type of &f is void (*)(int) and as such,
expects exactly one argument. However, the default argument of f is
used.
Default parameters are part of the type.
No they're not. The type of the function f, above, is void
(int). Try it:
void (*pf)( int ) = &f ;
or even:
void (*pf)( int i = 42 ) = &f ;
The reason the default arguments are used in his case is because
the expression invoking the function has default arguments
associated with it.

Interesting; this code compiles in gcc but not on comeau or VC++ 2005.

int f( int p = 5 )
{
return p;

}

template <typename T>
T F( T fp )
{
return fp;

}

#include <iostream>

int main()
{
std::cout << F( f )() << "\n";

}

Ok, so who is wrong, GCC or Comeau + VC++ ?
gcc is wrong in this case - and acknowledges it:

"The use of default arguments in function pointers, function typedefs
and other places where they are not permitted by the standard is
deprecated and will be removed from a future version of G++"

from http://gcc.gnu.org/onlinedocs/gcc-4....cated-Features

Greg

Aug 5 '07 #5
Greg Herlihy wrote:
....
>Ok, so who is wrong, GCC or Comeau + VC++ ?

gcc is wrong in this case - and acknowledges it:

"The use of default arguments in function pointers, function typedefs
and other places where they are not permitted by the standard is
deprecated and will be removed from a future version of G++"

from http://gcc.gnu.org/onlinedocs/gcc-4....cated-Features
I think they only fixed half the bug.

It appears that it only seems to work sometimes.

int f1( int p = 5 )
{
return p;
}

int f2( int p )
{
return p;
}

double d1( double p )
{
return p;
}

double d2( double p = 1.1 )
{
return p;
}

template <typename T>
T F( T fp )
{
return fp;
}

#include <iostream>

int main()
{

std::cout << F( f1 )() << "\n";
std::cout << F( f2 )() << "\n";

std::cout << F( d1 )() << "\n";
std::cout << F( d2 )() << "\n";
}
Aug 5 '07 #6
Gianni Mariani wrote:
Greg Herlihy wrote:
...
>>Ok, so who is wrong, GCC or Comeau + VC++ ?

gcc is wrong in this case - and acknowledges it:

"The use of default arguments in function pointers, function typedefs
and other places where they are not permitted by the standard is
deprecated and will be removed from a future version of G++"

from
http://gcc.gnu.org/onlinedocs/gcc-4....cated-Features

I think they only fixed half the bug.
Bug filed:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32993
Aug 5 '07 #7
Gianni,

Thanks for the solution! It seems to work with our compiler.
The reason the default arguments are used in his case is because
the expression invoking the function has default arguments
associated with it.
James,

What do you mean by that last sentence?. The expression invoking the
function is (&f)(). How are the default arguments associated with it?

Regards,
Claudiu

Aug 5 '07 #8
On Aug 5, 2:13 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
James Kanze wrote:
[Note that as far as I can tell, this comment has nothing to
do with what preceding in the thread.]
Interesting; this code compiles in gcc but not on comeau or VC++ 2005.
int f( int p = 5 )
{
return p;
}
template <typename T>
T F( T fp )
{
return fp;
}
#include <iostream>
int main()
{
std::cout << F( f )() << "\n";
}
Ok, so who is wrong, GCC or Comeau + VC++ ?
What operator<< is being called? The type of F(f) is "int (*)(
int )" (a "function" type is treated as a pointer to function
type when it is a function parameter). There's no such
operator<< in the standard, and there's no implicit conversion
from a function pointer to anything which has an operator<<, so
a priori, it shouldn't compile.

What does the output look like? Could it be that g++ is
(illegally) converting the function pointer to void*?

--
James Kanze (GABI Software) email:james.kan ze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 5 '07 #9
On Aug 5, 1:59 pm, claudiu <claudiu.ver... @gmail.comwrote :
Gianni,
Thanks for the solution! It seems to work with our compiler.
The reason the default arguments are used in his case is because
the expression invoking the function has default arguments
associated with it.
What do you mean by that last sentence?. The expression invoking the
function is (&f)(). How are the default arguments associated with it?
I'm not too sure of the exact rules myself, but the expression f
designates a function for which a default argument is provided.
While not part of the type, default arguments do propagate
through expressions, remaining attached to the expression.

Consider, for example:

int f( int = 42 ) ;
int g( int ) ;

// ....
assert( typeid( f ) == typeid( g ) ) ;

But of course, f(), (&f)(), etc. remain legal.

--
James Kanze (GABI Software) email:james.kan ze:gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1573
by: Ajax Chelsea | last post by:
1,int (*f)(int a = 1);//is it legal? 2,int (*f)(int a = 1); int g(int a = 2){return 0;} f = &g;//is it legal?
11
2586
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5 )),
10
2318
by: Dirk Vanhaute | last post by:
I have only small knowledge of c++, but I would like to compile the example in http://support.microsoft.com/kb/q246772/ HOWTO: Retrieve and Set the Default Printer in Windows I included "#include <Windows.h>" at the start, and the following goes wrong : BOOL DPGetDefaultPrinter(LPTSTR pPrinterName, LPDWORD pdwBufferSize) { ....
4
5372
by: Peter Salzman | last post by:
Hi all, Since PHP is a (weakly) typed language, is it possible to overload functions based on the type of their argument? Also, are there function pointers in PHP? Here's why I'm asking. Consider these two function:
27
2449
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to...
7
1845
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*); int bar(int *, char *, VFPTR, void *);
6
2690
by: CS | last post by:
Hi, How do you pass a function pointer as an argument? I have the following: class FReport { ... };
3
3633
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take...
1
1531
by: bwaichu | last post by:
What is the ansi correct way to pass a function when the argument is a function pointer? void foo(void (*)()); foo(read); or foo(&read);
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.