C
claudiu
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 FunctionWithLotsOfArgs(int, int = 0, double = 1.0, etc);
// read params p1, p2,... from XmL
BOOST_TYPEOF(&FunctionWithLotsOfArgs) fcn = &FunctionWithLotsOfArgs;
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, ...) = &FunctionWithLotsOfArgs;
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<typename T>
class FuncPtr
{
public:
FuncPtr(const T& ptr) : ptr_(ptr_){}
const T& operator()() const { return ptr_; }
private:
T ptr_;
};
template<typename T>
FuncPtr<T> MakeFuncPtr(const T& ptr)
{
return FuncPtr<T>(ptr);
}
void f(int i = 0);
int main()
{
MakeFuncPtr(&f)()();
}
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 FunctionWithLotsOfArgs(int, int = 0, double = 1.0, etc);
// read params p1, p2,... from XmL
BOOST_TYPEOF(&FunctionWithLotsOfArgs) fcn = &FunctionWithLotsOfArgs;
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, ...) = &FunctionWithLotsOfArgs;
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<typename T>
class FuncPtr
{
public:
FuncPtr(const T& ptr) : ptr_(ptr_){}
const T& operator()() const { return ptr_; }
private:
T ptr_;
};
template<typename T>
FuncPtr<T> MakeFuncPtr(const T& ptr)
{
return FuncPtr<T>(ptr);
}
void f(int i = 0);
int main()
{
MakeFuncPtr(&f)()();
}