Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #328

Defect report: constexpr and mutable members of literal types

From "Richard Smith"<richard@metafoo.co.uk>
Newsgroups comp.std.c++
Subject Defect report: constexpr and mutable members of literal types
Date 2011-10-19 15:23 -0700
Organization unknown
Message-ID <37522.10.0.7.178.1319061795.squirrel@webmail.secure.aluminati.net> (permalink)

Show all headers | View raw


Hi,

The C++11 IS allows literal types to have mutable members. In particular,
programs like the following are legal:

struct MM {
   mutable int n;
};

template<int n>  struct S { int k = n; };

int f(int k) {
   constexpr MM m = { 0 }; // ok, MM is a literal type
   m.n = k;                // ok, m.n is mutable
   return S<m.n>().k;      // ok, an lvalue-to-rvalue conversion of
                           // a subobject of a constexpr object is
                           // a constant expression [expr.const]/2
}

There seem to be two natural ways to fix this. Either (a) an lvalue-to-rvalue
conversion on a mutable member of a constexpr object should be considered to
not be a constant expression, or (b) literal types should not be allowed to
have mutable members.

(b) seems like the better solution, since it allows constexpr objects to be
ROM-able, which the constexpr papers note as an explicit goal. The clang
compiler currently implements (b).

Thanks,
Richard


-- 
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextNext in thread | Find similar


Thread

Defect report: constexpr and mutable members of literal types "Richard Smith"<richard@metafoo.co.uk> - 2011-10-19 15:23 -0700
  Re: Defect report: constexpr and mutable members of literal types Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-10-25 09:59 -0700
    Re: Defect report: constexpr and mutable members of literal types Richard Smith<richard@metafoo.co.uk> - 2011-10-26 11:19 -0700

csiph-web