Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: static constants in a class Date: Fri, 16 Sep 2011 09:32:21 +1200 Lines: 50 Message-ID: <9df975F2gmU15@mid.individual.net> References: <87d3f2xk8t.fsf@dpt-info.u-strasbg.fr> <9ddu5qF2gmU2@mid.individual.net> <55639b4f-449a-470a-9438-80577304e136@k7g2000vbd.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net pB4yGYiXKLYfDciQ6vmrTQZsI10bKMZEGHQT4aXqmWL/qnJNhu Cancel-Lock: sha1:sMAZxxh/P7ssHSwTyJn78kujPtM= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: <55639b4f-449a-470a-9438-80577304e136@k7g2000vbd.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:10184 On 09/16/11 09:24 AM, Christopher wrote: > On Sep 15, 4:17 am, Ian Collins wrote: >> On 09/15/11 08:50 PM, Alain Ketterlin wrote: >>> Urs Thuermann writes: >> >>>> How should one define a constant, say of type int, in a class? The >>>> only way that worked for me in all cases is to delcare the constant in >>>> the class in the header file and define its value in the >>>> implementation file: >> >>>> ---- foo.hh ---- >>>> class Foo { >>>> public: >>>> static const int N; >>>> ... >>>> }; >>>> ---- foo.cc ---- >>>> #include "foo.hh" >>>> const int Foo::N = 10; >>>> ---------------- >>> [...] >> >>> Make it a inline static method: >> >>> class Foo { >>> ... >>> inline static int N() { return 10; } >>> ... >>> }; >> >> The inline is unnecessary, but even without it the solution isn't valid: >> Foo::N() isn't a compile time constant. > > Where did the OP require it to be a _compile_ time constant? ---- bar.cc ---- void f() { int array[N]; for (int i = 0; i < N; i++) { ... } ---------------- > I do not understand the template solution proposed, what are the > internal workings that make it advantagous? It was unnecessarily complex. The correct solution was the one posted by "Marc". -- Ian Collins