Re: [eigen] proposal to use static const integer class members instead of enum values

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


On Sat, 13 Dec 2014, Benoit Jacob wrote:

2014-12-13 10:47 GMT-08:00 Marc Glisse <marc.glisse@xxxxxxxx>:

On Sat, 13 Dec 2014, Benoit Jacob wrote:

 Eigen does this for storing integer constants on a class:

class C {
 enum { SomeConstant = SomeValue };
};

Most other C++ code (including libstdc++ and libc++) do this instead:

class C {
 static const some_integer_type SomeConstant = SomeValue;
};


You are missing the extra:

const some_integer_type C::SomeConstant;

Otherwise, passing this value by reference may yield undefined references,
especially at -O0.


That would be the case for a static-storage non-constant integer, but not
for a static-storage constant integer, see:
http://stackoverflow.com/questions/9219898/why-can-you-initialize-a-static-const-variable-inline-but-not-a-plain-static-c

You misread my message. I am not complaining about the initialization but about the lack of definition.

$ cat r.cc
struct A {
  static const int i = 42;
};
void f(int const&) { }
int main() {
  f(A::i);
}
$ g++ r.cc
/tmp/ccqlsWDU.o: In function `main':
r.cc:(.text+0xf): undefined reference to `A::i'
collect2: error: ld returned 1 exit status


I don't think libstdc++ uses that much. If SomeConstant is "value", it
tries to derive from integer_constant instead.


True, I was forgetting that it used that. I don't remember the motivation
for that idiom, though. Does it make the compiler's job easier? Should we
do the same?

It avoids having to define the variable 'value' for each of them. And it automatically brings in any niceties like implicit conversion to that type, operator() in C++14, etc. But that's mostly useful for some special forms of traits, for Eigen it may not make as much sense.

--
Marc Glisse



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/