C++ 使用整数类型的模板,候选模板被忽略

C++ working with templates for integral types, candidate template is ignored

我有一个 class 和一个超载的 operator + 用于添加。我希望用它来添加一个整数类型及其整数成员。但是,候选模板被忽略了。正确的做法是什么?

#include <iostream>

using namespace std;

    template <class T>
    class A
    {
    public:
        A(uint32_t a = 0)
        : _a(a)
        { }

        template <class TT, typename std::enable_if<std::is_integral<TT>::value>::type>
        TT operator + (TT right) { return _a + right; }

    private:
        uint32_t _a;
    };

    class AT : public A<AT>
    {
    public:
        AT() : A(10) { }
    };
int main()
{
    AT at;
    cout<< (at + 10);

    return 0;
}

第二个模板参数typename std::enable_if<std::is_integral<TT>::value>::type声明了一个匿名模板参数,在at + 10的调用中无法推导。

我想你想要的可能是

// specify default value for the 2nd template parameter
template <class TT, typename std::enable_if<std::is_integral<TT>::value>::type* = nullptr>
T operator + (TT right) { return _a + right; }

LIVE