简体   繁体   中英

Compilation errors when attempting to use SFINAE (std::enable_if) and template specialization

Consider the code:

#include <type_traits>

struct CByteArray {};
struct HLVariant {
    HLVariant() {}
    HLVariant(const HLVariant&) {}
    HLVariant(const CByteArray&) {}

    };

template <typename T>
inline CByteArray serialize(const typename std::enable_if<true, T>::type& value)
{
    return serialize(HLVariant(value));
}

template <typename T>
inline CByteArray serialize(const typename std::enable_if<false, T>::type& value)
{
    return CByteArray();
}

template <>
inline CByteArray serialize(const HLVariant& value)
{
    return CByteArray();
}

int main()
{
    serialize(0);
    serialize(CByteArray());
    serialize(HLVariant());

    return 0;
}

Microsoft Visual Studio 2013 gives the following error:

C2912: explicit specialization 'CByteArray serialize(const HLVariant &)' is not a specialization of a function template

error C2783: 'CByteArray serialize(const std::enable_if::type &)' : could not deduce template argument for 'T'

The error suggests that there's no template <typename T> CByteArray serialize(const T&); function visible to the compiler, and I don't understand why. Note that I'm simply using true and false for enable_if condition for testing purposes here.

I've also tried this way instead ( enable_if on return type instead of argument):

    template <typename T>
inline typename std::enable_if<true, CByteArray>::type serialize(const T& value)
{
    return serialize(HLVariant(value));
}

template <typename T>
inline typename std::enable_if<false, CByteArray>::type serialize(const T& value)
{
    return CByteArray();
}

Now the error is:

C2039: 'type' : is not a member of 'std::enable_if<false,CByteArray>'

typename std::enable_if<true, T>::type is not deductible in your context (which T should the compiler test, it may be an infinite type for the general case).

You may use instead:

template <typename T>
inline
typename std::enable_if</*your condition depending of T*/, CByteArray>::type
serialize(const T& value)
{
    // Your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM