Topic: c++0x: literal types in constexpr. Relaxing rules? (simple to do)
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 15 Sep 2010 17:24:18 CST Raw View
On 15 Sep., 22:09, german diago <germandi...@gmail.com> wrote:
> I'm trying to do something with overloading in c++0x and with
> constexpr.
I'm not 100% sure, what you are saying here, but you *cannot*
overload on constexpr (But you can apply sfinae to make different
overloads mutually exclusive).
> The standard says that function parameters for constexpr functions
> will be of literal type. And literals are defined in the grammar as
> floating-point, integer... etc AND user-defined literals. I think that
> constexpr functions should allow
> types with constexpr constructors, without needing to create a user-
> defined literal. The restriction does not make sense in my opinion,
> and it imposes problems to generality.
Are you arguing for function parameters/return types that are
*references* or for non-reference types? This makes very
much of a difference.
A single constexpr constructor is *not* sufficient for any
"by-value" return/parameter, because the call will implicitly
invoke a copy/move c'tor and destructor - these must therefore
be trivial.
I agree that the current constraint of reference types to be
references to literal types is a pain and should be relaxed.
It means that not all valid C++03 constant expressions
(e.g. reference-constant expression) are properly supported.
Example:
#include <string>
static std::string s; // 's' is a reference-constant expression
template<class T>
constexpr bool ignore(T&&) { return true; }
static_assert(ignore(s), "Ouch"); // Ill-formed, because T is
// not a literal type!
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: David Krauss <potswa@gmail.com>
Date: Wed, 15 Sep 2010 17:49:14 CST Raw View
On Sep 15, 3:09=A0pm, german diago <germandi...@gmail.com> wrote:
> I'm trying to do something with overloading in c++0x and with
> constexpr.
> The standard says that function parameters for constexpr functions
> will be of literal type. And literals are defined in the grammar as
> floating-point, integer... etc AND user-defined literals. I think that
> constexpr functions should allow
> types with constexpr constructors, without needing to create a user-
> defined literal. The restriction does not make sense in my opinion,
> and it imposes problems to generality.
Literal type doesn't demand that any literal operator has actually
been defined; it is the prerequisite to defining one. It is defined in
3.9/10:
A type is a literal type if it is: =97 a scalar type; or
=97 a class type (Clause 9) with
=97 a trivial copy constructor,
=97 no non-trivial move constructor,
=97 a trivial destructor,
=97 a trivial default constructor or at least one constexpr constructor
other than the copy or move constructor, and
=97 all non-static data members and base classes of literal types;
or
=97 an array of literal type.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: german diago <germandiago@gmail.com>
Date: Wed, 15 Sep 2010 14:09:21 CST Raw View
I'm trying to do something with overloading in c++0x and with
constexpr.
The standard says that function parameters for constexpr functions
will be of literal type. And literals are defined in the grammar as
floating-point, integer... etc AND user-defined literals. I think that
constexpr functions should allow
types with constexpr constructors, without needing to create a user-
defined literal. The restriction does not make sense in my opinion,
and it imposes problems to generality.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]