Re: [eigen] Why do const accessors return by value? |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Why do const accessors return by value?
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Wed, 1 Apr 2009 02:45:57 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=4YlZFue/RPjVfeuopGXeD/HEennx/JGiSUbCW6rsGGo=; b=KkmD+Nm1xgSfbIzJjLDaIFPitk/seXEIxWiWWWXZuihDiemfMSXGyv/z83DLEs/ks8 4A0XrNSS/YuZf81ZaBHEUWLP/DFhwIcvT8/yZnYm0bCZxMGjAhOT9cmgWfKIiYGsTd5q /71wzrj98ler7nr5dssWazXx/j8lihiz/UevY=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=JTPBN6228jL3B5BaXA1NJ4t9UXvHckkNXgN7eZaGfNVg3WZozrc32Q/7fm6dJNKyJ8 LQ78+EPuRChi5m5dTmWSFel97e+ZyBamQbGdSqIo30KzOPx28pmPZOwGA/HKpM+JvAIq HGlQWQdeRW1JxagJyMkZISljc4n2Lu/U9PlpI=
2009/3/31 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>:
> 2009/3/31 Patrick Mihelich <patrick.mihelich@xxxxxxxxx>:
>> OK, right. But you do not always have to use the lowest-common-denominator
>> behavior (return by value). MatrixBase knows the Derived type and can return
>> by const-reference for Derived types with dense storage (Matrix, Map).
>
> ah right, good idea. I'll look into that.
>
>> OK but this is more verbose, and it's rather surprising that it doesn't do
>> the same thing as m(i,j) or even m.coeff(i,j). I do not think Matrix should
>> override the coeff() methods from MatrixBase with different signatures; more
>> generally I'd suggest that for any MatrixBase method foo, m.foo() and
>> m.derived().foo() should always behave identically.
>
> I understand. Good idea. I do that and I update you....
It's done now (in trunk).
I have actually taken a shortcut compared to the traditional c++ way
of doing this as you exposed.
Namely, Eigen expressions already had a DirectAccess flag which means
that coeffRef is available and that the data is stored
piecewise-contiguously in memory (piecewise meaning that a "stride" is
allowed between consecutive rows/cols).
So what I did was define a CoeffReturnType in MatrixBase, that is
either "const Scalar&" or "Scalar" depending on whether the xpr has
the DirectAccess flag, that's all.
That has the advantage of being a small local modification and not
requiring any more boilerplate code in all xpr classes.
>
>> BTW, there is a mistake in MapBase: const Scalar coeff(int index) const
>> should return const Scalar&.
>
> woops, good catch !
part of the same commit.
Cheers,
Benoit