mardi 29 mars 2016

static_assert triggering during function name resolution

The following code will static_assert when calling the member function A::Get with a string literal, presumably because the function overloading naming resolution must instantiate the template regardless of whether it is selected.

template<typename T>
struct helper
{
    static_assert(std::is_integral<T>::value, "Must be integeral type");

    typedef T type;
};

class A
{
public:
    A()
    {}

    std::string Get(const char* value) const
    {
        return std::string(value) + " (non-template)";
    }

    template<typename T>
    typename helper<T>::type Get(T value) const
    {
        return value;
    }
};

I can stop the assert by adding a 'helper' specialization, however there are other situations where the helper class is used, and it doesn't make sense to specialize for 'const char*' in other situations.

Is there some way to stop the helper::type from being instantiated with 'const char*' in this case? If not, what is a better way to design the helper class to avoid this problem?

Aucun commentaire:

Enregistrer un commentaire