Sleicreider
Sleicreider

Reputation: 57

constant expression function in c++98

I have following problem with c++98 constant expressions. Here is an example for an template struct.. which will receive the size at compile time..

Is it somehow possible to get this size as constant expression without c++11 constexpr? Take a look at GetCount()...

    template <typename ValueType, UInt32 size>
    struct FixedArray
    {
       ValueType mArr[size > 0 ? size : 1];
       UInt32 GetCount() const { return size; }

      ...
      other code
      ..
    }

I would like to be able to do something like this:

FixedArray<int , 10> a;
FixedArray<int , a.GetSize()> b;

Edit:

I couldn't find a way for C++ 98, seems like it isn't possible at all.

Upvotes: 1

Views: 1406

Answers (2)

Sleicreider
Sleicreider

Reputation: 57

Seems like there is no way to do this in C++ 98, it simply doesn't work.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234885

You could use the old-fashioned enum metaprogramming trick:

template <typename ValueType, UInt32 size>
struct FixedArray
{
    enum {value = size};
    // All your other stuff, but ditch your GetCount() function.
};

Then,

FixedArray<int, 10> a;
FixedArray<int, a.value> b;

will compile under C++98.

Upvotes: 0

Related Questions