W
wkaras
I would like to propose the following changes to the C++ Standard,
the goal of which are to provide an improved ability to specify
the constraints on type parameters to templates. Let me say from
the start that my knowledge of compiler implementation is very
limited. Therefore, my suggestions may have to be rejected
because they are difficult or impossible to implement.
The proposal is based on the concept of "type similarity". Type
A is said to be similar to type B if any usage of B is a valid
usage of A, and any usage of an instance of B is also a valid
usage of an instance of A. I suggest that the language be
amended to allow type parameters to templates to be required
to be similar to a previously declared class. The proposed
new syntax is show in this example:
template <typename A ~ class B>
class X
{
...
};
Class B must have been declared, but it may or may not be
completely defined in the program.
One obvious shortcoming of this idea is illustrated by
the template:
template <typename A>
class X
{
public:
static int foobar(A &a)
{ return(a.foo(a.bar())); }
};
A constraint on A is that A::bar() must return the same
type as the parameter to A::foo(). But requiring A to
be similar to a specific type would "over-constrain"
A::bar() to return and A::foo() to receive a specific
type. Therefore, I also propose that type parameters
can be constrained to be similar to class templates,
as illustrated by this example:
template <typename T>
class B
{
private:
B();
public:
T bar();
int foo(T t);
};
template <typename A ~ template <typename T> class B>
class X
{
public:
static int foobar(A &a)
{ return(a.foo(a.bar())); }
};
A type A is similar to a class template B if there is
an instantiation of B to which A is similar.
It would also be useful if the "implied" parameters
to the contraining template could be used in the
body of the template being declared, allowing
for something like:
template <typename T, typename R>
class B
{
private:
B();
public:
T bar();
R foo(T t);
};
template <
typename A ~ template <typename T, typename R> class B>
class X
{
public:
// Return value of foobar is the same as the return value of
// A::foo(), whatever that may be.
static R foobar(A &a)
{ return(a.foo(a.bar())); }
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
the goal of which are to provide an improved ability to specify
the constraints on type parameters to templates. Let me say from
the start that my knowledge of compiler implementation is very
limited. Therefore, my suggestions may have to be rejected
because they are difficult or impossible to implement.
The proposal is based on the concept of "type similarity". Type
A is said to be similar to type B if any usage of B is a valid
usage of A, and any usage of an instance of B is also a valid
usage of an instance of A. I suggest that the language be
amended to allow type parameters to templates to be required
to be similar to a previously declared class. The proposed
new syntax is show in this example:
template <typename A ~ class B>
class X
{
...
};
Class B must have been declared, but it may or may not be
completely defined in the program.
One obvious shortcoming of this idea is illustrated by
the template:
template <typename A>
class X
{
public:
static int foobar(A &a)
{ return(a.foo(a.bar())); }
};
A constraint on A is that A::bar() must return the same
type as the parameter to A::foo(). But requiring A to
be similar to a specific type would "over-constrain"
A::bar() to return and A::foo() to receive a specific
type. Therefore, I also propose that type parameters
can be constrained to be similar to class templates,
as illustrated by this example:
template <typename T>
class B
{
private:
B();
public:
T bar();
int foo(T t);
};
template <typename A ~ template <typename T> class B>
class X
{
public:
static int foobar(A &a)
{ return(a.foo(a.bar())); }
};
A type A is similar to a class template B if there is
an instantiation of B to which A is similar.
It would also be useful if the "implied" parameters
to the contraining template could be used in the
body of the template being declared, allowing
for something like:
template <typename T, typename R>
class B
{
private:
B();
public:
T bar();
R foo(T t);
};
template <
typename A ~ template <typename T, typename R> class B>
class X
{
public:
// Return value of foobar is the same as the return value of
// A::foo(), whatever that may be.
static R foobar(A &a)
{ return(a.foo(a.bar())); }
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]