digitalmars.D.learn - Overload function template for rectangular array
- John Chapman (7/7) May 25 2020 Is it possible to overload a function template for rectangular
- =?UTF-8?Q?Ali_=c3=87ehreli?= (18/23) May 25 2020 import std.traits;
Is it possible to overload a function template for rectangular
arrays? Is there any way to tell them apart from normal ones?
void foo(T)(T[] a) {}
void foo(T)(T[][] a) {}
auto ra = new int[][](5, 5);
ra.foo(); // matches both
Thanks for any hints.
May 25 2020
On 5/25/20 1:20 AM, John Chapman wrote:
void foo(T)(T[] a) {}
void foo(T)(T[][] a) {}
auto ra = new int[][](5, 5);
ra.foo(); // matches both
import std.traits;
void foo(T)(T[] a)
if (!isArray!T) {}
void foo(T)(T[] a)
if (isArray!T) {}
Or you can take T as parameter and check ElementType!T:
import std.traits;
import std.range;
void foo(T)(T a)
if (!isArray!(ElementType!T)) {}
void foo(T)(T a)
if (isArray!(ElementType!T)) {}
void main() {
auto ra = new int[][](5, 5);
ra.foo();
}
Ali
May 25 2020








=?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com>