模板和implcit构造函数的类定义之外的朋友声明

时间:2017-08-04 22:03:15

标签: c++ templates metaprogramming

我希望我的重载运算符+适用于混合类型。 非模板类没有问题。 为了使其与模板类一起使用,我在课程中添加了朋友操作符+,它可以正常工作。

template <typename T> class Wrapper
{
    T _val;
public:
    Wrapper(T val) :_val(val){}
    T getValue() const { return _val; }

    friend Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2)
    {
        return Wrapper<T>(val1.getValue() + val2.getValue());
    }
};

int main()
{
    Wrapper<int> v1 = 10; // OK, implicit constructor
    Wrapper<int> v2(20);
    Wrapper<int> result1 = v1 + v2; // OK, both values are of type Wrapper<int>
    Wrapper<int> result2 = v1 + 40; // Ok, casting by implicit constructor works
    return 0;
}

现在我想在类之外移动operator +实现,如下所示:

#include <iostream>
#include <string>

template <typename T> class Wrapper;

template <typename T> Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2);

template <typename T> class Wrapper
{
    T _val;
public:
    Wrapper(T val) :_val(val){}
    T getValue() const { return _val; }

    friend Wrapper<T> operator+ <>(const Wrapper<T>& val1, const Wrapper<T>& val2);
};

// template<class T> Wrapper<T> operator+(const Wrapper<T>&, const Wrapper<T>&)
// template argument deduction/substitution failed
template <typename T> Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2)
{
    return Wrapper<T>(val1.getValue() + val2.getValue());
}

int main()
{
    Wrapper<int> v1 = 10; // OK, implicit constructor
    Wrapper<int> v2(20);
    Wrapper<int> result1 = v1 + v2; // OK, both values are of type Wrapper<int>

    // note: mismatched types 'const Wrapper<T>' and 'int'
    Wrapper<int> result2 = v1 + 40; // Error
    return 0;

}

但是它给了我编译错误(将它们粘贴到上面的代码中)。 有可能解决它吗?

http://cpp.sh/5j5zg(工作) http://cpp.sh/9saow(不工作)

1 个答案:

答案 0 :(得分:1)

使这种朋友的功能不是模板功能有点奇怪。您可以通过使friend operator+成为模板函数来改变它以使其工作:

template <typename T>
class Wrapper
{
    T _val;
public:
    Wrapper(T val) :_val(val){}
    T getValue() const { return _val; }

    template <typename U>
    friend Wrapper<U> operator+(const Wrapper<U>& val1, const Wrapper<U>& val2);
};

template <typename T>
Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2)
{
    return Wrapper<T>(val1.getValue() + val2.getValue());
}

这几乎可以按预期工作,但隐式转换被禁用,因为这是一个模板函数。但是,将它们添加回来很容易:

template <typename T>
Wrapper<T> operator+(const Wrapper<T>& val1, const T& val2)
{
    return Wrapper<T>{ val1.getValue() + val2 };
}

template <typename T>
Wrapper<T> operator+(const T& val1, const Wrapper<T>& val2)
{
    return Wrapper<T>{ val1 + val2.getValue() };
}

Live on Coliru