元编程: is_floating_point<Tp>

本文探讨了如何使用C++的元编程技术来判断一个类型是否为浮点数类型,包括浮点和双精度浮点数。
摘要由CSDN通过智能技术生成

判断是否为浮点数类型

#include <iostream>
#include <string>
#include <functional>
#include <type_traits>

void void_print()
{
    return;
}

bool bool_print()
{
    return false;
}

char char_print()
{
    return 'a';
}

unsigned char uchar_print()
{
    return static_cast<unsigned char>(10);
}

short short_print()
{
    return 100;
}

unsigned short ushort_print()
{
    return 100;
}

int int_print()
{
    return -1;
}

unsigned int uint_print()
{
    return 0;
}

long long_print()
{
    return 10;
}

unsigned long ulong_print()
{
    return 10;
}

long long longlong_print()
{
    return 10;
}

unsigned long long ulonglong_print()
{
    return 10;
}

float float_print()
{
    return 1.0f;
}

double double_print()
{
    return 1.0;
}

std::string string_print()
{
    return std::string("abc");
}

const char* chs = "const char";
const char* const_char_print()
{
    return chs;
}

class A
{
public:
    A()
    {
        a = false;
        b = 'a';
        c = static_cast<unsigned char>(10);
        d = 11;
        e = 12;
        f = 1;
        g = 2;
        h = 3;
        i = 4;
        j = 5;
        k = 6;
        l = 7.0f;
        m = 8.0;
        n = "std::string";
        o = "const char";
    }

    virtual ~A()
    {
        std::cout << __FUNCTION__ << ": " << __LINE__ << ": run here " << std::endl;
    }

    void void_print()
    {
        return;
    }

    bool bool_print()
    {
        return a;
    }

    char char_print()
    {
        return b;
    }

    unsigned char uchar_print()
    {
        return c;
    }

    short short_print()
    {
        return d;
    }

    unsigned short ushort_print()
    {
        return e;
    }

    int int_print()
    {
        return f;
    }

    unsigned int uint_print()
    {
        return g;
    }

    long long_print()
    {
        return h;
    }

    unsigned long ulong_print()
    {
        return i;
    }

    long long longlong_print()
    {
        return j;
    }

    unsigned long long ulonglong_print()
    {
        return k;
    }

    float float_print()
    {
        return l;
    }

    double double_print()
    {
        return m;
    }

    std::string string_print()
    {
        return n;
    }

    const char* const_char_print()
    {
        return o;
    }

public:
    virtual A* creat_object()
    {
        return new A();
    }

    virtual std::string print_typename()
    {
        return std::string("A");
    }

public:
    bool a;
    char b;
    unsigned char c;
    short d;
    unsigned short e;
    int f;
    unsigned int g;
    long h;
    unsigned long i;
    long long j;
    unsigned long long k;
    float l;
    double m;
    std::string n;
    const char* o;
};

class B : public A
{
public:
    B() : A()
    {

    }

public:
    virtual A* create_object()
    {
        return new B();
    }

    virtual std::string print_typename()
    {
        return std::string("B");
    }
};

int main()
{
    std::cout << "This Dome: is_floating_point" << std::endl;

    bool a;
    char b;
    unsigned char c;
    short d;
    unsigned short e;
    int f;
    unsigned int g;
    long h;
    unsigned long i;
    long long j;
    unsigned long long k;
    float l;
    double m;
    std::string n;
    const char* o = "const char";

    a = false;
    b = 'a';
    c = static_cast<unsigned char>(10);
    d = 11;
    e = 12;
    f = 1;
    g = 2;
    h = 3;
    i = 4;
    j = 5;
    k = 6;
    l = 7.0f;
    m = 8.0;
    n = "std::string";

    // Step1:
    if(std::is_floating_point<void>::value)
    {
        std::cout << __LINE__ << ": is floating point type " << std::endl;
    }
    else
    {
        std::cout << __LINE__ << ": isn't floating point type" << std::endl;
    }

    if(std::is_floating_point<bool>::value)
    {
        std::cout << __LINE__ << ": is floating point type " << std::endl;
    }
    else
    {
        std::cout << __LINE__ <<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值