模板问题。编译器无法推断出模板参数

时间:2019-07-11 13:55:39

标签: c++

我真的没有什么可添加的,问题由错误消息描述。我只是不知道如何解决它以及为什么此代码无效。

struct BaseECSComponent;
typedef void* EntityHandle;
typedef uint32_t(*fpECSComponentCreate)(std::vector<char>&, EntityHandle, BaseECSComponent*);
typedef void(*fpECSComponentDelete)(BaseECSComponent*);

template<typename T>
uint32_t ECSComponentCreate(std::vector<char> & memory, EntityHandle entity, BaseECSComponent* baseComponent) {
    uint32_t freeSpot = memory.size();
    memory.reserve(freeSpot + sizeof(T));
    T* component = new(&memory[freeSpot]) T(*((T*)baseComponent));
    component->entity = entity;
    return freeSpot;
}
template<typename T>
void ECSComponentDelete(BaseECSComponent* baseComponent) {
    T* component = (T*)baseComponent;
    component->~T();
}

struct BaseECSComponent {
    template<typename T>
    static uint32_t RegisterType(fpECSComponentCreate fpCreate, fpECSComponentDelete fpDelete, size_t size) {
        static uint32_t componentID = 0;
        componentTypes.push_back(
            std::tuple< fpECSComponentCreate, fpECSComponentDelete, size_t >(
                fpCreate, fpDelete, size)
        );
        return componentID++;
    }

    static fpECSComponentCreate Get_fpCreate(uint32_t id) { return std::get<0>(componentTypes[id]); }
    static fpECSComponentDelete Get_fpDelete(uint32_t id) { return std::get<1>(componentTypes[id]); }
    static size_t GetSize(uint32_t id) { return std::get<2>(componentTypes[id]); }

    EntityHandle entity = nullptr;
private:
    static std::vector < std::tuple < fpECSComponentCreate, fpECSComponentDelete, size_t > > componentTypes;
};
std::vector < std::tuple < fpECSComponentCreate, fpECSComponentDelete, size_t > > BaseECSComponent::componentTypes;

template<typename T>
struct ECSComponent : public BaseECSComponent {
    static const fpECSComponentCreate   fpCreate;
    static const fpECSComponentDelete   fpDelete;
    static const uint32_t               id;
    static const size_t                 size;
};
template<typename T>
const fpECSComponentCreate ECSComponent<T>::fpCreate(ECSComponentCreate<T>);
template<typename T>
const fpECSComponentDelete ECSComponent<T>::fpDelete(ECSComponentDelete<T>);
template<typename T>
const uint32_t ECSComponent<T>::id(BaseECSComponent::RegisterType(ECSComponentCreate<T>, ECSComponentDelete<T>, sizeof(T)));
template<typename T>
const size_t ECSComponent<T>::size(sizeof(T));

错误输出:

错误C2672:“ BaseECSComponent :: RegisterType”:找不到匹配的重载函数 错误C2783:'uint32_t BaseECSComponent :: RegisterType(fpECSComponentCreate,fpECSComponentDelete,size_t)':无法推断出'T'的模板参数

0 个答案:

没有答案