Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #14882 > unrolled thread
| Started by | Samkit Jain <samkit.feeds@gmail.com> |
|---|---|
| First post | 2012-03-11 03:40 -0700 |
| Last post | 2012-03-16 09:46 +0900 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.c++
constepxr | Isn't this a constant expression? Samkit Jain <samkit.feeds@gmail.com> - 2012-03-11 03:40 -0700
Re: constepxr | Isn't this a constant expression? darylew@gmail.com - 2012-03-12 06:22 -0700
constepxr | Isn't this a constant expression? Johannes Schaub - litb <schaub.johannes@googlemail.com> - 2012-03-13 23:52 -0700
Re: constepxr | Isn't this a constant expression? 88888 Dihedral <dihedral88888@googlemail.com> - 2012-03-14 20:03 -0700
Re: constepxr | Isn't this a constant expression? Samkit Jain <samkit.feeds@gmail.com> - 2012-03-15 10:04 -0700
Re: constepxr | Isn't this a constant expression? Johannes Schaub <schaub.johannes@googlemail.com> - 2012-03-16 23:16 +0100
Re: constepxr | Isn't this a constant expression? Samkit Jain <samkit.feeds@gmail.com> - 2012-03-18 08:09 -0700
Re: constepxr | Isn't this a constant expression? Miles Bader <miles@gnu.org> - 2012-03-16 09:46 +0900
| From | Samkit Jain <samkit.feeds@gmail.com> |
|---|---|
| Date | 2012-03-11 03:40 -0700 |
| Subject | constepxr | Isn't this a constant expression? |
| Message-ID | <b673c645-fdbc-439d-8371-3fa938835798@ud10g2000pbc.googlegroups.com> |
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;
}
[toc] | [next] | [standalone]
| From | darylew@gmail.com |
|---|---|
| Date | 2012-03-12 06:22 -0700 |
| Message-ID | <10078092.2831.1331558537859.JavaMail.geo-discussion-forums@vbgu10> |
| In reply to | #14882 |
On Sunday, March 11, 2012 6:40:42 AM UTC-4, Samkit Jain wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Johannes Schaub - litb <schaub.johannes@googlemail.com> |
|---|---|
| Date | 2012-03-13 23:52 -0700 |
| Message-ID | <14420784.9.1331707952189.JavaMail.geo-discussion-forums@vbuf25> |
| In reply to | #14882 |
The address of local nonstatic variables are not a constant expression.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-03-14 20:03 -0700 |
| Message-ID | <18828563.57.1331780593588.JavaMail.geo-discussion-forums@pbcru10> |
| In reply to | #14926 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Samkit Jain <samkit.feeds@gmail.com> |
|---|---|
| Date | 2012-03-15 10:04 -0700 |
| Message-ID | <27b3d11c-36e5-4166-899a-3c2792cab6a7@t8g2000pbe.googlegroups.com> |
| In reply to | #14934 |
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");
[toc] | [prev] | [next] | [standalone]
| From | Johannes Schaub <schaub.johannes@googlemail.com> |
|---|---|
| Date | 2012-03-16 23:16 +0100 |
| Message-ID | <jk0e34$sdh$1@news.albasani.net> |
| In reply to | #14940 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Samkit Jain <samkit.feeds@gmail.com> |
|---|---|
| Date | 2012-03-18 08:09 -0700 |
| Message-ID | <f29ff57d-6897-44a7-977a-74646e0bb7d5@lf20g2000pbb.googlegroups.com> |
| In reply to | #14952 |
On Mar 17, 3:16 am, Johannes Schaub <schaub.johan...@googlemail.com>
wrote:
> 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.
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;
[toc] | [prev] | [next] | [standalone]
| From | Miles Bader <miles@gnu.org> |
|---|---|
| Date | 2012-03-16 09:46 +0900 |
| Message-ID | <874ntpl6ea.fsf@catnip.gol.com> |
| In reply to | #14926 |
Johannes Schaub - litb <schaub.johannes@googlemail.com> writes: > 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 -- Friendship, n. A ship big enough to carry two in fair weather, but only one in foul.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web