让函数返回C ++类中的任何类型

时间:2014-12-20 17:05:08

标签: c++

我上过这堂课:

#define TYPE_INVALID 0x00
#define TYPE_BYTE    0x01
#define TYPE_SHORT   0x02
#define TYPE_INT     0x03
#define TYPE_LONG    0x04
#define TYPE_FLOAT   0x05
#define TYPE_DOUBLE  0x06

class BASIC_TYPE
{
    private:
        int8_t  type;
        int8_t  byteValue;
        int16_t shortValue;
        int32_t intValue;
        int64_t longValue;
        float   floatValue;
        double  doubleValue;

    public:
        BASIC_TYPE();
        template<typename T> BASIC_TYPE(int8_t, T);

        template<typename T> void set(T);
        template<typename T> T    get();
};

BASIC_TYPE::BASIC_TYPE()
{
    type = TYPE_INVALID;
}

template<typename T> BASIC_TYPE::BASIC_TYPE(int8_t newType, T value)
{
    type = newType;
    set(value);
}

template<typename T> void BASIC_TYPE::set(T value)
{
    switch(type)
    {
        case TYPE_BYTE   : byteValue   = value; break;
        case TYPE_SHORT  : shortValue  = value; break;
        case TYPE_INT    : intValue    = value; break;
        case TYPE_LONG   : longValue   = value; break;
        case TYPE_FLOAT  : floatValue  = value; break;
        case TYPE_DOUBLE : doubleValue = value; break;
    }
}

template<typename T> T BASIC_TYPE::get()
{
    switch(type)
    {
        case TYPE_BYTE   : return byteValue;
        case TYPE_SHORT  : return shortValue;
        case TYPE_INT    : return intValue;
        case TYPE_LONG   : return longValue;
        case TYPE_FLOAT  : return floatValue;
        case TYPE_DOUBLE : return doubleValue;
    }
}

现在我想使用get() - 函数输出存储的数字,如下所示:

BASIC_TYPE val1(TYPE_INT, 1234);
BASIC_TYPE val2(TYPE_DOUBLE, 3.1415926535);

val1.set(5678);
val2.set(2.7182818284);
printf("%d\n%f\n", val1.get(), val2.get());

但是g ++说no matching function call to 'BASIC_TYPE::get() - 函数有printf,而template argument deduction/substitution failedcouldn't deduce template parameter 'T')。{/ p>

需要更改哪些代码才能正确编译?

3 个答案:

答案 0 :(得分:6)

  

&#34;需要更改哪些代码才能正确编译?&#34;

在这种情况下你需要明确。只需写下

printf("%d\n%f\n", val1.get<int>(), val2.get<double>());
                        // ^^^^^            ^^^^^^^^

C ++无法仅通过返回类型来区分各种get()函数实例化(其他方面与set()可以区分T作为参数类型)。
在这种情况下,您需要指定类型以明确地实例化模板化函数(如上所示)。


另请注意:我更倾向于使用enum代替那些#define TYPE_xxx语句:

enum MySupportedTypes {
    TYPE_INVALID ,
    TYPE_BYTE    ,
    TYPE_SHORT   ,
    TYPE_INT     ,
    TYPE_LONG    ,
    TYPE_FLOAT   ,
    TYPE_DOUBLE  ,
};

对于get()模板函数的实现,您应该考虑类似 1 的内容,以避免为不适当的请求类型调用get()函数。

template<typename T> T BASIC_TYPE::get() {
    switch(type) {
        case TYPE_BYTE: 
            std::is_same<T,int8_t> ? return byteValue : throw std::bad_typeid;
        case TYPE_SHORT: 
            std::is_same<T,int16_t> ? return shortValue : throw std::bad_typeid;
        //  analogous ...
    }
}

或者甚至更好地提供一种在编译时捕获类型不匹配的机制。


1)请参阅std::is_same

的文档参考

答案 1 :(得分:0)

您希望通过返回类型重载,C ++并不能让您轻松完成。但是,我会指出,你的最后一个例子需要你告诉它你想要的类型,因为参数可以是任何类型。即使C ++让你通过返回类型重载,它仍然不知道你想要什么类型。

那就是说,你可以使用一个丑陋的技巧来重载返回类型。我知道它可以用于此,同时查看如何通过返回类型重载,在这里:Function overloading by return type?

看哪:

class BASIC_TYPE
{
    // Your stuff, then:
    operator int (){ if (type != TYPE_INT) throw std::bad_typeid; return intValue; }
    operator float (){ if (type != TYPE_FLOAT) throw std::bad_typeid; return floatValue; }
    // And so on for every supported type
}

有了这个,你甚至不需要get函数,你只需调用值本身:

BASIC_TYPE val1(TYPE_INT, 1234);

//float bad = val1; // throws bad_typeid
int good = val1; // sets good to 1234

您可以使用强制转换来获取您想要的特定类型,以防您被转换为错误的类型。

如果重载构造函数和赋值运算符,你甚至可以摆脱声明类型的丑陋语法:

class BASIC_TYPE
{
    public:
        // Constructors
        BASIC_TYPE(int i):type(TYPE_INT),intValue(i){}
        // So on for the other types
        // Assignment
        BASIC_TYPE & operator=(int i){ if (type != TYPE_INT) throw std::bad_typeid; intValue = i; return *this;} 
}

BASIC_TYPE intType (1234); // TYPE_INT chosen autmatically
// intType = 1.0; // throws
intType = 4321; 

答案 2 :(得分:0)

无需为六种不同类型存储六个不同的数据成员。它们可以合并为一个联盟。此外,可以使用模板元编程设置type标志。无需在构造函数中明确地这样做。在使用union时,必须确保get函数的模板参数与对象的实际类型匹配。在这种情况下,我选择只返回模板参数类型的零初始值,如果该类型与对象的类型不匹配。您可以选择抛出异常。

#include <iostream>
#include <type_traits>

#define TYPE_INVALID 0x00
#define TYPE_BYTE    0x01
#define TYPE_SHORT   0x02
#define TYPE_INT     0x03
#define TYPE_LONG    0x04
#define TYPE_FLOAT   0x05
#define TYPE_DOUBLE  0x06

using namespace std;

class BASIC_TYPE
{
    private:
        int8_t  type;
        union
        {
            int8_t  byteValue;
            int16_t shortValue;
            int32_t intValue;
            int64_t longValue;
            float   floatValue;
            double  doubleValue;
        }
        data;

    public:
        BASIC_TYPE();
        template<typename T> BASIC_TYPE(T);
        template<typename T> void set(T);
        template<typename T> T    get();
};

BASIC_TYPE::BASIC_TYPE()
{
    type = TYPE_INVALID;
}

template<typename T> BASIC_TYPE::BASIC_TYPE(T value)
{
    set(value);
}

template<typename T> void BASIC_TYPE::set(T value)
{
    if (is_same<T, int8_t>::value)
    {   
        type = TYPE_BYTE;
        data.byteValue = value;
    }   
    else if (is_same<T, int16_t>::value)
    {
        type = TYPE_SHORT;
        data.shortValue = value;
    }
    else if (is_same<T, int32_t>::value)
    {
        type = TYPE_INT;
        data.intValue = value;
    }
    else if (is_same<T, int64_t>::value)
    {
        type = TYPE_LONG;
        data.longValue = value;
    }
    else if (is_same<T, float>::value)
    {
        type = TYPE_FLOAT;
        data.floatValue = value;
    }
    else if (is_same<T, double>::value)
    {
        type = TYPE_DOUBLE;
        data.doubleValue = value;
    }
    else
        type = TYPE_INVALID;

}

template<typename T> T BASIC_TYPE::get()
{
    T ret{};
    if (type == TYPE_BYTE && is_same<T, int8_t>::value)
        ret = data.byteValue;
    else if (type == TYPE_SHORT && is_same<T, int16_t>::value)
        ret = data.shortValue;
    else if (type == TYPE_INT && is_same<T, int32_t>::value)
        ret = data.intValue;
    else if (type == TYPE_LONG && is_same<T, int64_t>::value)
        ret = data.longValue;
    else if (type == TYPE_FLOAT && is_same<T, float>::value)
        ret = data.floatValue;
    else if (type == TYPE_DOUBLE && is_same<T, double>::value)
        ret = data.doubleValue;

    return ret;
}