digitalmars.D.learn - Template Trick
- matovitch (12/12) Jun 12 2013 Hello,
- H. S. Teoh (8/15) Jun 12 2013 [...]
- matovitch (3/16) Jun 12 2013 This was a mistake (it's quite late) :
- matovitch (22/22) Jun 12 2013 To be more precise the code below doesn't compile :
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/32) Jun 12 2013 Here is one way:
- matovitch (2/11) Jun 12 2013 Thank you !
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/21) Jun 12 2013 Oops. It should be:
- bearophile (8/14) Jun 12 2013 I think something like that is not yet possible, but maybe it
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/19) Jun 12 2013 You later corrected that it should be
Hello,
I got a simple vector template :
struct Vector(T, uint N)
{
alias type T;
T data[N];
}
And I'd like to call a function like :
void func(V, V.type default_value)(args...);
But this (of course) doesn't work. Is there a simple and nice way
to do this ? (I'm sure there is ;-))
Thanks.
Jun 12 2013
On Wed, Jun 12, 2013 at 11:26:40PM +0200, matovitch wrote:
Hello,
I got a simple vector template :
struct Vector(T, uint N)
{
alias type T;
[...]
This line should read:
alias type = T;
And it should work as you wanted.
T
--
If you look at a thing nine hundred and ninety-nine times, you are perfectly
safe; if you look at it the thousandth time, you are in frightful danger of
seeing it for the first time. -- G. K. Chesterton
Jun 12 2013
On Wednesday, 12 June 2013 at 21:36:38 UTC, H. S. Teoh wrote:On Wed, Jun 12, 2013 at 11:26:40PM +0200, matovitch wrote:This was a mistake (it's quite late) : alias T type;Hello, I got a simple vector template : struct Vector(T, uint N) { alias type T;[...] This line should read: alias type = T; And it should work as you wanted. T
Jun 12 2013
To be more precise the code below doesn't compile :
struct Vector(T, uint N)
{
alias T type;
enum dimension = N;
T data[N];
}
void func(V, V.type def_val) (uint i, V v)
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}
void main()
{
alias Vector!(int, 3) Vec3i;
Vec3i v;
func!(Vec3i, 42)(2, v);
}
With the error message :
test.d(8): Error: no property 'type' for type 'V'
test.d(8): Error: V.type is used as a type
Jun 12 2013
On 06/12/2013 02:47 PM, matovitch wrote:
To be more precise the code below doesn't compile :
struct Vector(T, uint N)
{
alias T type;
enum dimension = N;
T data[N];
}
void func(V, V.type def_val) (uint i, V v)
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}
void main()
{
alias Vector!(int, 3) Vec3i;
Vec3i v;
func!(Vec3i, 42)(2, v);
}
With the error message :
test.d(8): Error: no property 'type' for type 'V'
test.d(8): Error: V.type is used as a type
Here is one way:
void func(V, alias def_val) (uint i, V v)
if (is (typeof(def_val == V.type)))
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}
Ali
Jun 12 2013
On Wednesday, 12 June 2013 at 21:52:46 UTC, Ali Çehreli wrote:
Here is one way:
void func(V, alias def_val) (uint i, V v)
if (is (typeof(def_val == V.type)))
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}
Ali
Thank you !
Jun 12 2013
On 06/12/2013 02:56 PM, matovitch wrote:On Wednesday, 12 June 2013 at 21:52:46 UTC, Ali Çehreli wrote:Oops. It should be: if (is (typeof(def_val) == V.type)) Hmmm. How come the other one worked as well? Because the type of (def_val == V.type) is compiled as bool and since bool is a valid type, 'is' passes.Here is one way: void func(V, alias def_val) (uint i, V v) if (is (typeof(def_val == V.type)))Ali{ if (i < v.dimension) { v.data[i] = def_val; } } AliThank you !
Jun 12 2013
matovitch:
void func(V, V.type def_val) (uint i, V v)
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}
I think something like that is not yet possible, but maybe it
will be possible later.
Your code has also allowed me to find a new small compiler bug
that I have just filed:
http://d.puremagic.com/issues/show_bug.cgi?id=10346
Bye,
bearophile
Jun 12 2013
On 06/12/2013 02:26 PM, matovitch wrote:> Hello,I got a simple vector template : struct Vector(T, uint N) { alias type T;You later corrected that it should be alias T type; But still, prefer the new syntax over the backward C syntax: alias type = T;T data[N]; } And I'd like to call a function like : void func(V, V.type default_value)(args...);So, that function is already defined and you are trying to call it? How are you calling it? What is the error message?But this (of course) doesn't work. Is there a simple and nice way to do this ? (I'm sure there is ;-))It is not clear to me what the purpose is. :) Ali
Jun 12 2013









=?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> 