Exercise: Translation C++ -> D

mki none at none.com
Sun May 18 14:39:22 PDT 2008


I am struggling with the D template syntax.
After some thought, my problem can be formulated to translate the following small C++ program to D:

// begin C++ code ###
#include <iostream>

template<class T> class A {};

template<class T> class B {
        static void tellMe() {
            std::cout << "generic." << std::endl;

template<class T> class B<A<T> > {
        static void tellMe() {
            std::cout << "specialized, size of T is " << sizeof(T) << std::endl;

int main() {
    typedef int T1;
    typedef A<int> T2;

This C++ program compiles without error, the output of the compiled programm is as expected:
specialized, size of T is 4

Now my attempt to write the same in D:

//begin D code
import std.stdio;

class A(T) { }

class B(T) {
    static void tellMe() {

class B(TT : A!(T)) {
    static void tellMe() {
        writefln("specialized, sizeof T is ",T.sizeof);

void main() {
    alias int T1;
    alias A!(int) T2;
//end D code

This does not compile, the error is
test3.d(13): Error: undefined identifier T

In C++, the definition of the identifiers which appear in the template specialization expression are defined after the preceding template keyword (template< ... HERE ...> ...). But I don't see how this is done in D.

Please someone show me the (probably very simple) solution I am overlooking.



More information about the Digitalmars-d-learn mailing list