类函数的模板变量返回类型

时间:2015-02-09 18:54:02

标签: c++

我试图创建一个用于解释各种位图类型的类(例如24位rgb或16位单色......等等)。我对模板不是很好,所以也许有人可以告诉我为什么这会给我一堆错误?

    enum PixelOptions {
        I8, I16, I32, I64, F32, F64
    };

    template <PixelOptions T>
    class BitmapInterpreter {
        uint32_t Width;
        uint32_t Height;
        void* Data;

    public:
        BitmapInterpreter(uint32_t Width, uint32_t Height, void* Data) {
            this->Width = Width;
            this->Height = Height;
            this->Data = Data;
        }


        uint8_t* Pixel<I8>(const uint32_t &X, const uint32_t &Y) {
            return nullptr;
        }
        uint16_t* Pixel<I16>(const uint32_t &X, const uint32_t &Y) {
            return nullptr;
        }
        uint32_t* Pixel<I32>(const uint32_t &X, const uint32_t &Y) {
            return nullptr;
        }
    };

功能只是占位符。基本上我希望它根据声明的方式返回根据x和y计算的变量类型指针。我想加上&lt;模板选项&gt;函数会这样做,但编译器只是说&#34;缺少;之前&lt;&#34;在定义第一个Pixel函数之后。

编辑:PS!模板参数必须是我可以存储在变量中的东西,例如枚举(为什么我不使用模板类型)

3 个答案:

答案 0 :(得分:1)

使返回类型取决于非类型模板参数Pixel T。一个例子是为此目的创建一个辅助类:

template <PixelOptions T>
struct RetType;

template <>
struct RetType<I8>
{
 using type = uint8_t;
};

///More specializations go here.

将原始方法更改为:

typename RetType<T>::type* Pixel(const uint32_t &X, const uint32_t &Y) {
            return nullptr;
        }

答案 1 :(得分:0)

您可以专门使用该方法,例如:

template <PixelOptions T> struct PixelType;

template <> struct PixelType<I8> {
    using type = uint8_t;
};

template <> struct PixelType<I16> {
    using type = uint8_t;
};

template <> struct PixelType<I32> {
    using type = uint16_t;
};

template <PixelOptions T> class BitmapInterpreter
{
    uint32_t Width;
    uint32_t Height;
    void* Data;

public:
    BitmapInterpreter(uint32_t Width, uint32_t Height, void* Data)
    {
        this->Width = Width;
        this->Height = Height;
        this->Data = Data;
    }

    typename PixelType<T>::type* Pixel(const uint32_t& X, const uint32_t& Y);
};
template <> typename PixelType<I8>::type* BitmapInterpreter<I8>::Pixel(const uint32_t& X, const uint32_t& Y)
{
    return nullptr;
}
template <> typename PixelType<I16>::type* BitmapInterpreter<I16>::Pixel(const uint32_t& X, const uint32_t& Y)
{
    return nullptr;
}
template <> typename PixelType<I32>::type* BitmapInterpreter<I32>::Pixel(const uint32_t& X, const uint32_t& Y)
{
    return nullptr;
}

答案 2 :(得分:0)

您有模板化方法Pixel<>而未声明它们是模板。我想你想要做的是:

enum PixelOptions {
    I8, I16, I32, I64, F32, F64
};

// No need for template here, nothing else than one method is templated
class BitmapInterpreter {
    uint32_t Width;
    uint32_t Height;
    void* Data;

public:
    BitmapInterpreter(uint32_t Width, uint32_t Height, void* Data) {
        this->Width = Width;
        this->Height = Height;
        this->Data = Data;
    }


    // Declaring there is such a method
    template <PixelOptions T>
    uint8_t* Pixel(const uint32_t &X, const uint32_t &Y);
};

// Defining specific versions of the method
template <>
uint8_t* BitmapInterpreter::Pixel<I8>(const uint32_t &X, const uint32_t &Y) {
    return nullptr;
}
template <>
uint16_t* BitmapInterpreter::Pixel<I16>(const uint32_t &X, const uint32_t &Y) {
    return nullptr;
}
template <>
uint32_t* BitmapInterpreter::Pixel<I32>(const uint32_t &X, const uint32_t &Y) {
    return nullptr;
}

请注意,这仍然不会生成有效代码,因为返回类型在每种情况下都不同。你也需要模板化。

相关问题