[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: RFC, problem with g++4



Florian Weimer <fw@deneb.enyo.de> writes:

> * Goswin von Brederlow:
>
>>> This was once desirable because you couldn't declare real constants in
>>> classes.  Today,
>>>
>>>   template <typename T>
>>>   struct Foo
>>>   {
>>>     static const unsigned N = T::N;
>>>     char bar[N];
>>>   };
>>>
>>> works and the enum trick lost its importance.
>>
>> Doesn't that still make N a real variable in memory and does not get
>> optimized away like enums?
>
> Only if you provide a definition, and not just a declaration, it
> seems.

The proper use of this construct seems to be:

template <typename T>
struct Foo {
  static const unsigned N = T::N;
  char bar[N];
};

struct Bla {
  static const unsigned N;
};

const unsigned Bla::N = 10;

int main() {
  Foo<Bla> foo;
}


Correct me if I'm wrong but doesn't that

1.) Only move the const declaration from the template into the
template parameter?

2.) Cause the template to have static member N in every file that uses
the template and for every type?

3.) Cause Bla to have a static member N in every file that uses
the template and for every type?

4.) Cause 2 extra indirections due to 2+3?

By the way,

template <typename T>
struct Foo {
  static const unsigned N = 10;
  char bar[N];
};

works just as well.

MfG
        Goswin



Reply to: