Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.mixmin.net!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | "Richard Smith"<richard@metafoo.co.uk> |
| Newsgroups | comp.std.c++ |
| Subject | Proposal: constexpr, non-const member functions |
| Date | Tue, 8 Nov 2011 11:32:36 -0800 (PST) |
| Organization | unknown |
| Lines | 39 |
| Sender | std-cpp-request@vandevoorde.com |
| Approved | stephen.clamage@oracle.com |
| Message-ID | <46713.10.0.7.178.1320718071.squirrel@webmail.secure.aluminati.net> (permalink) |
| Reply-To | richard@metafoo.co.uk |
| NNTP-Posting-Host | +dwByeTiFaeBO05125J/PZ2auXh5bJp7494nOvAwm6k= |
| Content-Type | text/plain;charset=iso-8859-1 |
| X-Trace | news.albasani.net LiKI06kN5VULpLD066MWkVEjS19W7T1/hXDhFeRXQmk2ucJfVXCXE9RsUcUSkFjxT74PXU/XXk7Q7CLMo3vaMA== |
| X-Complaints-To | abuse@albasani.net |
| NNTP-Posting-Date | Tue, 8 Nov 2011 19:32:37 +0000 (UTC) |
| X-Mailer | Perl5 Mail::Internet v2.05 |
| X-Submission-Address | std-cpp-submit@vandevoorde.com |
| Cancel-Lock | sha1:f765HEhKluoBjcQaJl5s/2gR8pU= |
| X-Original-Date | Tue, 8 Nov 2011 02:07:51 -0000 (UTC) |
| Xref | x330-a1.tempe.blueboxinc.net comp.std.c++:361 |
Show key headers only | View raw
Hi,
Pointers and references to non-const objects can generally be used within
constant expressions in C++11. However, there is a roadblock in the way of
anyone who wants to use them as the 'this' pointer, because constexpr member
functions are implicitly const. Consider a trivial class like this:
template<typename T>
class Wrapper {
T v;
public:
constexpr Wrapper(const T&v) : v(v) {}
T&get() { return v; } // note, can't be constexpr
constexpr const T&get() { return v; }
// ...
};
This seems fine until you try to use a temporary of this type in a constant
expression:
constexpr int n = Wrapper<int>(0).get(); // ill-formed!
This doesn't work, because the non-const (and thus non-constexpr) get()
overload is selected.
Since C++11 is already out of the gate, it's too late to remove the 'constexpr
implies const' rule, so instead we could consider a syntax like this:
constexpr T&get() mutable { return v; }
Does this seem like a useful extension?
--
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 | Next — Next in thread | Find similar
Proposal: constexpr, non-const member functions "Richard Smith"<richard@metafoo.co.uk> - 2011-11-08 11:32 -0800
Re: Proposal: constexpr, non-const member functions Dave Abrahams <dave@boostpro.com> - 2011-11-08 14:45 -0800
Re: Proposal: constexpr, non-const member functions Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-11-08 14:47 -0800
Re: Proposal: constexpr, non-const member functions Marc <marc.glisse@gmail.com> - 2011-11-08 14:47 -0800
Re: Proposal: constexpr, non-const member functions Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-11-09 21:54 -0800
Re: Proposal: constexpr, non-const member functions Daniel Krügler<daniel.kruegler@googlemail.com> - 2011-11-09 23:14 -0800
csiph-web