D模板专门化在不同的源文件中 [英] D template specialization in different source file

查看:75
本文介绍了D模板专门化在不同的源文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了这个问题,该问题关于如何在D中模拟类型类,并提出了一种方法

I recently asked this question about how to simulate type classes in D and suggested a way to do this using template specialization.

我发现D无法识别其他源文件中的模板专业化。因此,我不能只对未定义通用函数的文件中包含的文件进行专门化处理。为了说明这一点,请考虑以下示例:

I discovered that D doesn´t recognize template specialization in a different source file. So I couldn´t just make a specialization in a file not included from the file where the generic function is defined. To illustrate, consider this example:

//template.d
import std.stdio;
template Generic(A) {
  void sayHello() {
    writefln("Generic");
  }
}

void testTemplate(A)() {
    Generic!A.sayHello();
}


//specialization.d
import std.stdio;
import Template;

template Generic(A:int) {
  void sayHello() {
      writefln("only for ints");
  }
}

void main() {
    testTemplate!int();
}

此代码在我运行时显示泛型。因此,我问是否有很好的解决方法,以便可以从算法中使用更专业的形式。

This code prints "generic" when I run it. So I´m asking whether there is some good workaround, so that the more specialized form can be used from the algorithm.

我在有关Type类的问题中使用的解决方法在导入所有带有模板特化文件的文件后混合通用函数,但这有点丑陋且受限制。

The workaround I used in the question about Type classes was to mixin the generic functions after importing all files with template specialization, but this is somewhat ugly and limited.

我听说c ++ 1x将具有extern模板,这将允许这样做。 D是否具有类似的功能?

I heard c++1x will have extern templates, which will allow this. Does D have a similar feature?

推荐答案

我认为我可以对此问题给出正确的答案。

I think I can give a proper answer to this question. No.

您要尝试的是劫持template.d的功能(大小写也应匹配并导入Template,某些操作系统很重要)。请考虑:

What you are trying to do is highjack the functionality of template.d (also case should match on file and import Template, some operating systems it matters). Consider:

// template.d
...

// spezialisation.d
import std.stdio;
import template;

void main() {
    testTemplate!int();
}

现在有人更新了代码:

// specialization.d
import std.stdio;
import template;
import helper;

void main() {
    testTemplate!int();
    getUserData();
}

完美吗?好帮手内部:

// helper.d
getUserData() { ... }


template Generic(A:int) {
    A placeholder; //...
}

您现在已经更改了specialization.d的行为从导入,实际上这将无法编译,因为它不能调用sayHello。这种高防措施确实有其问题。例如,您可能有一个采用Range的函数,但是您的库的使用者不能传递数组,除非您的库导入std.array,因为这是将数组转换为范围的地方。

You have now changed the behavior of specialization.d just from an import and in fact this would fail to compile as it can not call sayHello. This highjack prevention does have its issues. For example you may have a function which takes a Range, but the consumer of your library can not pass an array unless your library imports std.array since this is where an array is "transformed" into a range.

我没有解决您的问题的方法。

I do not have a workaround for your problem.

Michal的评论为第二种形式的劫机提供了一种解决方案,在这种情况下可以说是专业化。 d试图劫持getUserData

Michal's comment provides a solution to the second form of highjacking, where say specialization.d tried to highjack getUserData

// specialization.d
import std.stdio;
import template;
import helper;

alias helper.getUserData getUserData;

string getUserData(int num) { ... }

void main() {
    testTemplate!int();
    getUserData();
}

这篇关于D模板专门化在不同的源文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆