Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Richard Smith"<richard@metafoo.co.uk> |
|---|---|
| Newsgroups | comp.std.c++ |
| Subject | Proposal: constexpr, non-const member functions |
| Date | 2011-11-08 11:32 -0800 |
| Organization | unknown |
| Message-ID | <46713.10.0.7.178.1320718071.squirrel@webmail.secure.aluminati.net> (permalink) |
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