template makeConvMatrix(T...) { // wrapper added
string helper()
{
string result;
static foreach(t; T)
{
result ~= "\t" ~ t.stringof;
}
result ~= "\n";
static foreach(t1; T)
{
result ~= t1.stringof;
static foreach(t2; T)
{
result ~= "\t" ~ (is(t1:t2) ? "yes" : "no");
}
result ~= "\n";
}
return result;
}
enum makeConvMatrix = helper(); // eponymous call
}
extern(C) // for betterC
void main()
{
import core.stdc.stdio;
static immutable convMatrix = makeConvMatrix!(byte, ubyte, short, ushort, int, uint, long, ulong); // no more () there
printf("%s\n", convMatrix.ptr);
}
d的betterC版造转换矩
本文介绍了C++中一个名为`makeConvMatrix`的模板函数,该函数用于创建一个转换矩阵,展示不同类型的相互转换可能性。通过`helper`辅助函数实现,它遍历类型参数并生成一个表示类型间是否可转换的二维字符串矩阵。在示例中,该函数用于创建`byte`到`ulong`之间的转换矩阵,并打印输出。此技术展示了C++的模板元编程和类型推断能力,对于理解和优化C++代码具有重要意义。
摘要由CSDN通过智能技术生成