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 @   zjh6  阅读(10)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示