无法在Visual Studio 2017中明确专门化

问题描述 投票:0回答:1

我正在使用cocos2d-x在mac和windows上玩游戏。

我首先在Xcode中编写代码,可以在mac上运行。

我把它带到Windows并试图在Visual Studio 2017中构建时出错。

NRZNotification.h

#include "cocos2d.h"

class NRZNotification : public cocos2d::Ref
{
protected:
    std::string _name;
    cocos2d::Ref* _sender;

    ...

    cocos2d::ValueMap _valueMap;
    cocos2d::Map<std::string, cocos2d::Ref*> _objectMap;
public:
    const std::string& getName(){return _name;}
    cocos2d::Ref* getSender(){return _sender;}

    NRZNotification();
    virtual ~NRZNotification();
    static NRZNotification* create(const std::string& name, Ref* sender);
    bool init(const std::string& name, Ref* sender);

    ...

    template <typename T,
    typename std::enable_if<!std::is_convertible<T, cocos2d::Ref*>::value,
    std::nullptr_t>::type = nullptr>
    inline T getValue(const std::string& key)
    {
        //CCLOG("%s", __PRETTY_FUNCTION__);
        return 0;
    }
    template <typename T,
    typename std::enable_if<std::is_convertible<T, cocos2d::Ref*>::value,
    std::nullptr_t>::type = nullptr>
    inline T getValue(const std::string& key)
    {
        //CCLOG("%s", __PRETTY_FUNCTION__);
        return dynamic_cast<T>(_objectMap.at(key));
    }
};
#include "NRZNotification_Private.h"

NRZNotification_Private.h

#include "NRZNotification.h"

...

#pragma mark - get value

template <>
inline int NRZNotification::getValue<int,nullptr>(const std::string& key)
{
    if (_valueMap.find(key) == _valueMap.end()) {
        return 0;
    } else {
        return _valueMap.at(key).asInt();
    }
}
template <>
inline float NRZNotification::getValue(const std::string& key)
{
    if (_valueMap.find(key) == _valueMap.end()) {
        return 0.0f;
    } else {
        return _valueMap.at(key).asFloat();
    }
}
template <>
inline double NRZNotification::getValue(const std::string& key)
{
    if (_valueMap.find(key) == _valueMap.end()) {
        return 0.0;
    } else {
        return _valueMap.at(key).asDouble();
    }
}

...

这些代码在mac上成功运行,但在Visual Studio 2017中,调用getValue()会给出错误“无法显式专门化”。

getValue()是一个函数模板,根据返回值是否为cocos2d :: Ref的子类来划分实现。

此外,对int,float,string等进行了专门化。

我该如何修复此代码?

我正在使用cocos2d-x 3.17.1。

谢谢。

c++ visual-studio cocos2d-x sfinae
1个回答
1
投票

我冒昧地从你的代码中创建了一个MCVE。

#include <type_traits>

struct A {
    template<typename T, typename std::enable_if<!std::is_convertible<T, A>::value, int>::type = 0>
    T getValue() {
        return 1;
    }
    template<typename T, typename std::enable_if<std::is_convertible<T, A>::value, int>::type = 0>
    T getValue() {
        return T();
    }
};

template<>
inline int A::getValue<int, 0>() {
    return 3;
}

int main() {
    A a;
    return a.getValue<int>();
}

实际上,MSVC 2019无法编译它

<source>(15): error C2910: 'A::getValue': cannot be explicitly specialized

而GCC和clang编译得很好。 Live demo.

幸运的是,解决方案很简单 - 只需删除显式模板参数即可。无论如何它们都是多余的:

template<>
inline int A::getValue() {
    return 3;
}

所以对于你的情况删除<int,nullptr>

template <>
inline int NRZNotification::getValue<int,nullptr>(const std::string& key)
© www.soinside.com 2019 - 2024. All rights reserved.