d标准库与自写函数冲突

原文

module testcmpmodule;

//公共导入
public import std.algorithm, std.math, std.stdio;

import std.range, std.traits;

//函数重载组.
public import std.algorithm : cmp;
public import std.math      : cmp;

auto cmp(A, B)(in A a, in B b)
if(!(isInputRange!A && isInputRange!B)          //排除 std.algorithm.cmp
&& !(isFloatingPoint!A && isFloatingPoint!B))   //排除 std.math.cmp
{
  return a==b ? 0 : a<b ? -1 : 1; //保底方式
}

//发布实用程序模块的另一个模块:

module testcmpmodule2;

public import std.algorithm, testcmpmodule, std.stdio;
//这里删除std.algorithm,就可以了.

void w(A...)(in A a){ writeln(a); }
//主要模块:

import testcmpmodule2;

void main(){
  w(cmp(.1, .2));   //std.math.cmp
  w(cmp("a", "b")); //std.algorithm.cmp   
//<- 这里出错了
  w(cmp(1, 2));     //my cmp
}

std.string也会公开导入std.algorithm.cmp.

//其他模块,只能看见该标模块.
public import std.string, std.uni, std.algorithm, std.math;

import std.range, std.traits;

//重载组
public import std.algorithm : cmp;
public import std.math      : cmp;
...
posted @ 2022-08-12 09:41  zjh6  阅读(15)  评论(0)    收藏  举报  来源