View previous topic :: View next topic |
Author |
Message |
flashdog
Joined: 10 Oct 2008 Posts: 15
|
Posted: Mon Nov 10, 2008 11:43 pm Post subject: c++ template in D |
|
|
Hello,
how can I make this:
Code: |
/* File : example.h */
// Some template definitions
template<class T> T max(T a, T b) { return a>b ? a : b; }
template<class T> class vector {
T *v;
int sz;
public:
vector(int _sz) {
v = new T[_sz];
sz = _sz;
}
T &get(int index) {
return v[index];
}
void set(int index, T &val) {
v[index] = val;
}
};
|
avaible in D?
Best regards |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
aliloko
Joined: 31 Jan 2008 Posts: 15
|
Posted: Tue Nov 11, 2008 8:25 am Post subject: |
|
|
I would write :
Code: |
T max(T)(T a, T b)
{
return a>b ? a : b;
}
class Vector(T)
{
private:
T[] v;
public:
this(int sz)
{
v.length = sz;
}
void set(int index, T val)
{
v[index] = val;
}
T get(int index)
{
return v[index];
}
}
|
|
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
flashdog
Joined: 10 Oct 2008 Posts: 15
|
Posted: Wed Nov 12, 2008 12:40 am Post subject: |
|
|
Thank you for the code, but I meant more to make a binding to the c++ code so that I can use it under D and to rewrite it.
Is it possible to make a binding to the c++ code? |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
flashdog
Joined: 10 Oct 2008 Posts: 15
|
Posted: Wed Nov 12, 2008 12:41 am Post subject: |
|
|
Thank you for the code, but I meant more to make a binding to the c++ code so that I can use it under D and no to rewrite it.
Is it possible to make a binding to the c++ code? |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Wed Nov 12, 2008 5:58 am Post subject: |
|
|
It is possible but only in D2 and it's quite limited. |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
aliloko
Joined: 31 Jan 2008 Posts: 15
|
Posted: Wed Nov 12, 2008 11:14 am Post subject: |
|
|
flashdog wrote: | Thank you for the code, but I meant more to make a binding to the c++ code so that I can use it under D and to rewrite it.
Is it possible to make a binding to the c++ code? |
Oops, it's a miss. |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
aliloko
Joined: 31 Jan 2008 Posts: 15
|
Posted: Wed Nov 12, 2008 11:16 am Post subject: |
|
|
flashdog wrote: | Thank you for the code, but I meant more to make a binding to the c++ code so that I can use it under D and to rewrite it.
|
Oops, it was a miss. |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
clayasaurus
Joined: 21 May 2004 Posts: 857
|
Posted: Wed Nov 12, 2008 2:49 pm Post subject: |
|
|
http://www.digitalmars.com/d/2.0/cpp_interface.html
Quote: |
D templates have little in common with C++ templates, and it is very unlikely that any sort of reasonable method could be found to express C++ templates in a link-compatible way with D.
This means that the C++ STL, and C++ Boost, likely will never be accessible from D.
|
|
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
Trass3r
Joined: 29 Feb 2008 Posts: 66 Location: Germany
|
Posted: Thu Aug 12, 2010 7:57 am Post subject: |
|
|
clayasaurus already said it: no.
Templates aren't supported. |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
|