简体   繁体   English

模板友好功能和返回类型扣除

[英]Template friend function and return type deduction

Note: This question is really close to Return type deduction for in-class friend functions , but I did not find the answer to my problem there. 注意:这个问题非常接近于类内朋友函数的返回类型推导 ,但我没有在那里找到我的问题的答案。

Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z 用clang 3.4测试std = c ++ 1y和clang 3.5用std = c ++ 14和std = c ++ 1z

This code compiles: 此代码编译:

#include <iostream>

template<class T>
class MyClass {
    public:
        MyClass(T const& a) : impl(a) {}

        template<class T0, class T1> friend auto
        // requires operator+(T0,T1) exists
        operator+(MyClass<T0> const& a, MyClass<T1> const& b)
        {
            return MyClass<decltype(a.impl+b.impl)>{a.impl + b.impl};
        }

        T getImpl() const { return impl; }

    private:
        T impl;
};

int main() {
    MyClass<int> x(2);
    MyClass<long> y(2);

    auto z = x+y;
    std::cout << z.getImpl() << "\n";
}

Now if I define operator+ outside of the class, it does not compile anymore: 现在如果我在类之外定义operator +,它就不再编译了:

template<class T>
class MyClass {
    public:
        MyClass(T const& a) : impl(a) {}

        template<class T0, class T1> friend auto
        operator+(MyClass<T0> const& a, MyClass<T1> const& b);

        T getImpl() const { return impl; }
    private:
        T impl;
};

template<class T0, class T1> auto
operator+(MyClass<T0> const& a, MyClass<T1> const& b)
{
    return MyClass<decltype(a.impl+b.impl)>{a.impl + b.impl};
}

Clang 3.4 says: Clang 3.4说:

error: use of overloaded operator '+' is ambiguous (with operand types MyClass<int> and MyClass<long>)

And then points at what it believes to be two different functions: the declaration in the class and the definition outside the class. 然后指出它认为是两个不同的函数:类中的声明和类外的定义。

My question is: is it a clang bug, or just that template parameters are deduced for a friend function thus leading the two functions not being equivalent is some cases ? 我的问题是:它是一个铿锵的错误,还是仅仅为朋友函数推导出模板参数,从而导致两个函数不相等是某些情况? And what alternative would you suggest: make operator+ a member function, or define friend operator+ inside the class (which would in my opinion clutter the class interface) ? 你会建议什么样的选择:make operator +一个成员函数,或者在类中定义friend operator +(在我看来会混淆类接口)?

Just for your information, I have a real use case of such code, where I try to wrap a third -party matrix class and I need return type deduction because of the use of expression template for lazy evaluation. 仅仅为了您的信息,我有一个这样的代码的真实用例,我尝试包装第三方矩阵类,我需要返回类型推导,因为使用表达式模板进行延迟评估。

Edit : The following does work (but still clutters the interface...) 编辑 :以下工作(但仍然使界面混乱...)

template<typename T>
class MyClass
{
    T impl;

public:
    explicit MyClass(T a) : impl(std::move(a)) { }

    T const& getImpl() const { return impl; }

    template<typename T0, typename T1>
    friend auto operator +(MyClass<T0> const& a, MyClass<T1> const& b) -> MyClass<decltype(a.impl + b.impl)>;
};

template<typename T0, typename T1>
auto operator +(MyClass<T0> const& a, MyClass<T1> const& b) -> MyClass<decltype(a.impl + b.impl)>
{
    return MyClass<decltype(a.impl + b.impl)>(a.impl + b.impl);
}

edit: look at the comments section, it's a bug in gcc 4.8.2 and 4.9 编辑:查看评论部分,这是gcc 4.8.2和4.9中的错误

Gcc error code: Gcc错误代码:

prog.cpp:10:61: error: non-static data member declared 'auto' operator+(MyClass const& a, MyClass const& b) ^ prog.cpp: In function 'int main()': prog.cpp:25:15: error: no match for 'operator+' (operand types are 'MyClass' and 'MyClass') auto z = x+y; prog.cpp:10:61:错误:非静态数据成员声明'auto'运算符+(MyClass const&a,MyClass const&b)^ prog.cpp:在函数'int main()'中:prog.cpp:25:15 :错误:'operator +'不匹配(操作数类型是'MyClass'和'MyClass')auto z = x + y; ^ ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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