From: german diago on
Hi all. I've been reading the constexpr c++ proposal. The constexpr
proposal lets you evaluate expressions at compile-time. I wonder, if
it would be possible, as an improvement, to make constexpr parameters
able to be used for
parameters to templates. This could be useful, since in C++03 you
cannot do this today.

template <float val>
struct sqrt { ... };

You could do this if types with constexpr cosntructors were allowed as
template parameters. It could also be useful to pass strings (if they
had a constexpr constructor) for things like registering a class
without the use of tricky code:

template <class T, string id>
struct register_class {
private:
...
public:
constexpr register_class() ...
};

register_class<lib::a_class, "lib::a_class">();

Could this be possible. Is it feasible as an enhancement to current
constexpr proposal? Thanks in advance.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alberto Ganesh Barbati on
german diago ha scritto:
> Hi all. I've been reading the constexpr c++ proposal. The constexpr
> proposal lets you evaluate expressions at compile-time. I wonder, if
> it would be possible, as an improvement, to make constexpr parameters
> able to be used for
> parameters to templates. This could be useful, since in C++03 you
> cannot do this today.
>
> template <float val>
> struct sqrt { ... };
>
> You could do this if types with constexpr cosntructors were allowed as
> template parameters. It could also be useful to pass strings (if they
> had a constexpr constructor) for things like registering a class
> without the use of tricky code:
>
> template <class T, string id>
> struct register_class {
> private:
> ...
> public:
> constexpr register_class() ...
> };
>
> register_class<lib::a_class, "lib::a_class">();
>
> Could this be possible. Is it feasible as an enhancement to current
> constexpr proposal? Thanks in advance.
>

It may be possible in theory, but AFAIK it's not likely to happen. First
of all, support for floating point non-type parameter is totally
unrelated with constexpr. While there are compilers out there that
allows floating point parameters as an extension, the committee seems to
be oriented to stick with the C++03 restrictions. At least that's what
the most recent draft of C++0x says. Due to this reluctance, I find it
hard to believe that the committee would do the big step and allow all
literal types, given that they provide a much bigger challenge. Of
course, I may be wrong. On one thing I am certain, however, and it is
that strings will never be allowed because they are not and cannot be
literal types as they need to allocate their buffers on the heap.
However, please consider that you can already do this:

template <class T, const std::string& id>
struct register_class {
...
};

std::string a_class_id("lib::a_class");
register_class<lib::a_class, a_class_id>();

Just my opinion,

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Greg Herlihy on
On Mar 30, 12:44 pm, Alberto Ganesh Barbati <AlbertoBarb...(a)libero.it>
wrote:
> german diago ha scritto:
> > Hi all. I've been reading the constexpr c++ proposal. The constexpr
> > proposal lets you evaluate expressions at compile-time. I wonder, if
> > it would be possible, as an improvement, to make constexpr parameters
> > able to be used for
> > parameters to templates. This could be useful, since in C++03 you
> > cannot do this today.
>
> > template <float val>
> > struct sqrt { ... };
>
> > Could this be possible. Is it feasible as an enhancement to current
> > constexpr proposal? Thanks in advance.
>
> It may be possible in theory, but AFAIK it's not likely to happen. First
> of all, support for floating point non-type parameter is totally
> unrelated with constexpr.

The two concepts are in fact closely related - after all, it was the
adoption of the constexpr proposal that added support for floating-
point constant expressions tp C++. And once it became possible to use
a floating point value in a constant expression, then there was no
reason left why a floating point value could not also be used as a non-
type template parameter.

The constexpr proposal pretty much reached the same conclusion:

5.2 Non-type template parameter
"The suggestion of extending non-type template parameter type to
literal types will
be subject of an independent proposal."

See: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2007/n2235.pdf

Greg





--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: David Peklak on
>
> 5.2 Non-type template parameter
> "The suggestion of extending non-type template parameter type to
> literal types will
> be subject of an independent proposal."
>

I was reading the same paper and wonder if such a proposal actually
exsists already. I did not find it in the list of proposals...

However, I wonder how the equality of literal types would be
determined. For example, if there is a type with a constexpr
constructor, LitType,
in the template

template<LitType t>
class C {...} ;

would
C<a> and C<b> be the same type if a and b are equal? Or only if their
address is equal? How would equality determined?
By demanding that LitType defines an equality operator
bool LitType::operator == (LitType const&)
?

What if that operator is defined such that it is possible that three
(literal) values a, b, and c are defined such that a==b, b==c, but not
a==c ??

This sounds tricky...

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Gabriel Dos Reis on
David Peklak <dpeklak(a)gmail.com> writes:

| >
| > 5.2 Non-type template parameter
| > "The suggestion of extending non-type template parameter type to
| > literal types will
| > be subject of an independent proposal."
| >
|
| I was reading the same paper and wonder if such a proposal actually
| exsists already. I did not find it in the list of proposals...

It always existed in our minds since the beginning :-), and came up
repeatedly in discussions.

| However, I wonder how the equality of literal types would be
| determined. For example, if there is a type with a constexpr
| constructor, LitType,
| in the template
|
| template<LitType t>
| class C {...} ;
|
| would
| C<a> and C<b> be the same type if a and b are equal? Or only if their
| address is equal? How would equality determined?
| By demanding that LitType defines an equality operator
| bool LitType::operator == (LitType const&)
| ?
|
| What if that operator is defined such that it is possible that three
| (literal) values a, b, and c are defined such that a==b, b==c, but not
| a==c ??
|
| This sounds tricky...

You would probably notice that literal types are essentially regular
types, for which equality does not seal any mystery (even if we
allowed floating point types).


--
Dr. Gabriel Dos Reis (gdr(a)cs.tamu.edu), Assistant Professor
http://www.cs.tamu.edu/people/faculty/gdr/
http://www.open-axiom.org/
Texas A&M University -- Department of Computer Science


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]