d的betterC版造转换矩

本文介绍了C++中一个名为`makeConvMatrix`的模板函数,该函数用于创建一个转换矩阵,展示不同类型的相互转换可能性。通过`helper`辅助函数实现,它遍历类型参数并生成一个表示类型间是否可转换的二维字符串矩阵。在示例中,该函数用于创建`byte`到`ulong`之间的转换矩阵,并打印输出。此技术展示了C++的模板元编程和类型推断能力,对于理解和优化C++代码具有重要意义。
摘要由CSDN通过智能技术生成
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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值