constepxr | Isn't this a constant expression?

S

Samkit Jain

Compiler: gcc 4.6.1

Code:

struct data
{
constexpr data(int i) :i_(i) {}
int i_;
};

template <typename T, int Size>
constexpr void const* get_addr(T const (&elements)[Size]) { return
(void const*)elements; }

template <typename T>
constexpr void const* get_addr(T const& element) { return (void
const*)&element; }

int main()
{
constexpr data elements[] = { data(10) };
constexpr data ten(10);
static_assert(get_addr(elements) == get_addr(elements), "not equal
- 1"); // doesn't compile?
static_assert(elements == elements, "not equal - 2");
static_assert(get_addr(ten) == get_addr(ten), "not equal -
3"); // doesn't compile?
static_assert(&ten == &ten, "not equal - 4");
return 0;
}
 
D

darylew

Compiler: gcc 4.6.1

Code:

struct data
{
constexpr data(int i) :i_(i) {}
int i_;
};

template <typename T, int Size>
constexpr void const* get_addr(T const (&elements)[Size]) { return
(void const*)elements; }

template <typename T>
constexpr void const* get_addr(T const& element) { return (void
const*)&element; }

IIUC, constexpr stuff is only for value semantics. Addresses are for identity semantics, so constexpr is ignored. Compile-time evaluations are done generally before object addresses are finalized, so pointer stuff can't really ever be constexpr.

Daryle W.
 
J

Johannes Schaub - litb

The address of local nonstatic variables are not a constant expression.
 
8

88888 Dihedral

Johannes Schaub - litbæ–¼ 2012å¹´3月14日星期三UTC+8下åˆ2時52分31秒寫é“:
The address of local nonstatic variables are not a constant expression.



Johannes Schaub - litbæ–¼ 2012å¹´3月14日星期三UTC+8下åˆ2時52分31秒寫é“:
The address of local nonstatic variables are not a constant expression.



Johannes Schaub - litbæ–¼ 2012å¹´3月14日星期三UTC+8下åˆ2時52分31秒寫é“:
The address of local nonstatic variables are not a constant expression.



Johannes Schaub - litbæ–¼ 2012å¹´3月14日星期三UTC+8下åˆ2時52分31秒寫é“:
The address of local nonstatic variables are not a constant expression.

Yes, don't pass references and addresses in the STACK in a function return.

That's quite buggy for the stack frames.
 
S

Samkit Jain

The below lines are compiling fine. What does compiler thinks and what
values are evaluated at compile time?

static_assert(elements == elements, "not equal - 2");
static_assert(&ten == &ten, "not equal - 4");

Also, this compiles:

static_assert(get_addr("!23") == get_addr("123"), "not equal -
5");
 
M

Miles Bader

Johannes Schaub - litb said:
The address of local nonstatic variables are not a constant expression.

I dunno what the standard says, but his confusion seems understandable
to me.

After all, he's not taking the address of a random local variable, but
of a _constexpr_ local variable, and I think the most natural way of
thinking about such a thing is "local in scope but global in extent,"
exactly like a local static variable.

The idea of a variable with a value explicitly fixed at compile-time,
but an address that changes at runtime seems just sort of weird...

-miles
 
J

Johannes Schaub

Am 15.03.2012 18:04, schrieb Samkit Jain:
The below lines are compiling fine. What does compiler thinks and what
values are evaluated at compile time?

static_assert(elements == elements, "not equal - 2");
static_assert(&ten ==&ten, "not equal - 4");

This works because the address of a local variable is a core constant
expression (these can be thought of intermediary results within the
computation of a constant expression). The assignment then evaluates to
true and yields a constant expression bool integer.
 
S

Samkit Jain

Am 15.03.2012 18:04, schrieb Samkit Jain:



This works because the address of a local variable is a core constant
expression (these can be thought of intermediary results within the
computation of a constant expression). The assignment then evaluates to
true and yields a constant expression bool integer.

By this argument I should be able to instantiate another template
which expects a <void const*> as non-type template parameter, but I
can't.
Obviously, I am doing something wrong but I want to understand it
according to standard wordings.

template <void const* addr>
struct unique_data
{
};

unique_data<get_addr(ten)> u_obj;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top