让函数返回C ++类中的任何类型 [英] Let a function return any type in C++ class

查看:103
本文介绍了让函数返回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以输出存储的数字,如下所示:

Now I want to use the get()-function to output the stored number like this:

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 ++说有没有匹配的函数调用'BASIC_TYPE ::在 printf - 函数中的get(),并且模板参数扣除/替换失败无法推导出模板参数'T'

But g++ says that there is no matching function call to 'BASIC_TYPE::get() at the printf-function and that the template argument deduction/substitution failed (couldn't deduce template parameter 'T').

正确?

推荐答案


需要更改什么才能让代码正确编译?

"What has to be changed to let the code compile properly?"

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

You need to be explicit in this case. Just write

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

C ++不能区分各种 get()函数实例化只是通过返回类型(其他可以与 set()其中 T 显示为参数类型)。

在这种情况下,您需要指定类型以明确地实例化模板函数(如上所示)。

C++ cannot distinguish the various get() function instantiations just by return type (other as it can with set() where T appears as parameter type).
In such case, you'll need to specify the type to instantiate the templated function explicitely (as shown above).

另外请注意:我宁愿使用枚举 #define TYPE_xxx 语句:

Also note: I'd prefer to use an enum instead of those #define TYPE_xxx statements:

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

要实现 get()模板函数,你应该考虑像这样 1 ,以避免 get()函数被调用一个不适当的请求类型。

For the implementation of your get() template function, you should consider something like this1, to avoid the get() function being called for an inappropriate requested type.

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 ...
    }
}

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

Or even better provide a mechanism that will catch type mismatching at compile time.

1)请参阅 std :: is_same

1) See the documentation reference of std::is_same

这篇关于让函数返回C ++类中的任何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆