[Ns-developers] TimeUnit operator +

Gustavo Carneiro gjcarneiro at gmail.com
Sun Feb 18 05:18:33 PST 2007


  Something not so nice about our Time templates I discovered while playing
with python bindings:

When you do:

  Time t1 = Seconds(1);
  Time t2 = Seconds(2);

  double foo = (t1 + t2).GetSeconds();

  You get a compilation error:
    error: 'class ns3::TimeUnit<1>' has no member named 'GetSeconds'

  That's because (t1 + t2) invokes:
              template <int N> TimeUnit<N> operator + (TimeUnit<N> const
&lhs, TimeUnit<N> const &rhs);
for N=1.

  To get the result you want you need to do

  double foo = Time(t1 + t2).GetSeconds();

  Besides being a bit less elegant, it means that a temporary TimeUnit<1>
object is created and immediately discarded; needless overhead.  It would be
nicer if class Time was a simple alias to TimeUnit<1>.  Of course, how to do
this aliasing and add the GetSeconds() method only to the TimeUnit<1>
template instantiation and not for others I cannot say, but it sure would be
ideal.

  Comments?

-- 
Gustavo J. A. M. Carneiro
"The universe is always one step beyond logic."


More information about the Ns-developers mailing list