Can Schedule recognize a function with parameter that has a default value?

12 views
Skip to first unread message

John Young

unread,
Oct 11, 2016, 9:36:45 AM10/11/16
to ns-3-users
I find that it won't recognize  that,
for a function as following
ret fun1(p1,p2=a)

Simulator::schedule(Time duration, &fun1,x) would get me an error

And if I add another function as 
ret fun1(p1) 

I won't get an error.And the command above would invoke the first one. Why is not consistent with c++?

pdbarnes

unread,
Oct 11, 2016, 11:27:30 AM10/11/16
to ns-3-users
Excellent question. According to cppreference[1] there is only one function, f(int, int), say, declared as f(int, int =1). When the compiler encounters f(2) it silently expands to f(2, 1).

In the context of Schedule however, the EvenImpl caches the function pointer and any explicit arguments:
Schedule(delay, &f, 2) caches the address of f, a two-argument function, and the int value 2. In this translation unit that's fine. The compiler doesn't see the invocation through the function pointer; that happens in a different translation unit, for SimulatorImpl.

Your solution, create an explicit one argument overload for f, is the correct one.

HTH,
Peter
[1] http://en.cppreference.com/w/cpp/language/default_arguments

John Young

unread,
Oct 11, 2016, 8:24:03 PM10/11/16
to ns-3-users
Ok, thanks.

在 2016年10月11日星期二 UTC+8下午11:27:30,pdbarnes写道:
Reply all
Reply to author
Forward
0 new messages