Topic: c++0x: user-defined types as constexpr function arguments.


Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 15 Sep 2010 17:25:07 CST
Raw View
On 15 Sep., 22:09, german diago <germandi...@gmail.com> wrote:
> Hello. I would like to know if it's possible (g++ 4.6 shows an error)
> to use
> constexpr functions with user-defined types as arguments, as long as
> these
> types have constexpr constructors. Reading the standard draft I don't
> see
> if that's possible (I think a literal is needed, but I'm not sure).

Not a literal, but a literal type, see below.

> Something like this:
>
> template <class T>
> struct MyType {
> ...
> public:
>
>     constexpr MyType() ..
>
> };
>
> template <class T>
> constexpr bool saySomethingAboutMyType(MyType<T> t) {
>   return true;
> }
>
> This does not work, it shows an error about MyType<T> not being a
> literal type.

We need to see more of your type. A single constexpr
constructor is not sufficient for several reasons:

1) Because it is a template, whether this constructor is really a
constexpr constructor depends on what the constructor body
contains for the particular specialization. If this code does not
satisfy the constraints of a constexpr c'tor, the c'tor is not a
constexpr constructor.

2) The parameter types/return types of constexpr functions
must be literal types or references thereof. A literal type
requires trivial copy/move-constructors/destructor. I cannot
see whether your type satisfies this constraints from the
snippet-like code.

Last but not least, the standard wording for constexpr
function is currently under refactoring and gcc 4.6. will
have a *very* good testing base for nearly all of these
changes.

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: german diago <germandiago@gmail.com>
Date: Wed, 15 Sep 2010 14:09:14 CST
Raw View
Hello. I would like to know if it's possible (g++ 4.6 shows an error)
to use
constexpr functions with user-defined types as arguments, as long as
these
types have constexpr constructors. Reading the standard draft I don't
see
if that's possible (I think a literal is needed, but I'm not sure).

Something like this:

template <class T>
struct MyType {
...
public:

    constexpr MyType() ..
};

template <class T>
constexpr bool saySomethingAboutMyType(MyType<T> t) {
  return true;
}

This does not work, it shows an error about MyType<T> not being a
literal type.

Thanks for your time.



--
[ 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                      ]