Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using static constexpr within static constexpr

19 views
Skip to first unread message

bitrex

unread,
Apr 3, 2016, 5:25:52 PM4/3/16
to
So the following code compiles fine under gcc (C++14), to build an array
of squares:

#define F4(i) F1((i)), F1((i)+1), F1((i)+2), F1((i)+3)
#define F16(i) F4((i)), F4((i)+4), F4((i)+8), F4((i)+12)
#define F64(i) F16((i)), F16((i)+16), F16((i)+32), F16((i)+48)
#define F256(i) F64((i)), F64((i)+64), F64((i)+128), F64((i)+192)

struct Foo {
static constexpr unsigned int bar(char i)
{
return i*i;
}

#define F1(i) blarg(i)
unsigned int baz[256] =
{
F256(0)
};
#undef F1
};

Now suppose I wanted the array to be associated only with the
class/struct Foo, not individual instances. If I change the array
declaration to:

static const unsigned int baz[256] =
{
F256(0)
};

I get an error that the array must be declared "constexpr" for in-class
initialization. So then if I try:

static constexpr unsigned int baz[256] =
{
F256(0)
};

Nope, can't do that either, because it complains that I'm calling a
"constexpr" within a "constexpr."

I'd really only like one copy of the array data to be associated with
the class (i.e. static) and not consume memory for every object
instantiated, as they all require the same data. And I'd like the
contents of the array to be evaluated at compile time via the first
"constexpr."

Is there a way to get at what I'm getting at?

bitrex

unread,
Apr 3, 2016, 5:29:14 PM4/3/16
to
On 04/03/2016 05:25 PM, bitrex wrote:
> #define F1(i) blarg(i)

#define F1(i) bar(i), rather ;-)

Robert Wessel

unread,
Apr 4, 2016, 1:21:22 AM4/4/16
to
Is the definition of blarg() constexpr?

bitrex

unread,
Apr 4, 2016, 8:50:54 AM4/4/16
to
On 04/04/2016 01:21 AM, Robert Wessel wrote:

> Is the definition of blarg() constexpr?

blarg should = bar...made a mistake when typing up my dummy names...


0 new messages