调用重载与ref限定符成员函数是不明确的

调用重载与ref限定符成员函数是不明确的

本文介绍了调用重载与ref限定符成员函数是不明确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个奇怪的行为,当用 G ++ gcc 4.8.1和 MinGW $ c> -std = gnu ++ 1y flag)。在SSCCE的精神我孤立以下代码片段:

  struct C 
{

template< ;类型名X>
auto
f(X&&)const&
{; }

template<类型名X>
auto
f(X&&)&
{; }

template<类型名X>
auto
f(X&&)&&&
{; }

};

int main()
{
int i {};
#if 1
C {}。f(i);
#endif
#if 1
C c {};
c.f(i);
#endif
return 0;
}

出现错误:


$ b b

  main.cpp:在函数'int main()':
main.cpp:29:10:错误:调用重载的'f(int&是不明确的
cf(i);
^
main.cpp:29:10:注意:候选是:
main.cpp:6:5:note:auto C :: f(X&& [with X = int&]
f(X&&)const&
^
main.cpp:11:5:note:auto C :: f(X&&& [with X = int&]
f(X&&)&
^
main.cpp:16:5:note:auto C :: f(X&&&&&&&& [with X = int&]
f(X&&&&&&&&&&&
^

但在 #if 1 #if 0 #if 0 #if 1 它编译正常。此外,如果我用 void 替换所有 auto ,那么所有编译成功。

$

解决方案

g ++ 4.8.2具有错误,同样的问题更简单():

  struct A {
auto f()& {}
auto f()&&& {}
};

int main(){
A {}。f();
A a;
a.f();尽管程序显然是正确的,但是
}

它似乎是ref-qualifier和返回类型推导之间的交互中的一个错误:推断过程是在将它们移交给重载解析之前从限定符去除隐式对象参数。



我已将此举报为。 p>

I found a strange behaviour, when compliling my code with G++ (gcc 4.8.1 and MinGW 4.8.2 with -std=gnu++1y flag). In spirit of SSCCE I isolating the following snippet:

struct C
{

    template< typename X >
    auto
    f(X &&) const &
    { ; }

    template< typename X >
    auto
    f(X &&) &
    { ; }

    template< typename X >
    auto
    f(X &&) &&
    { ; }

};

int main()
{
    int i{};
#if 1
    C{}.f(i);
#endif
#if 1
    C c{};
    c.f(i);
#endif
    return 0;
}

It gives an error:

main.cpp: In function 'int main()':
main.cpp:29:10: error: call of overloaded 'f(int&)' is ambiguous
     c.f(i);
          ^
main.cpp:29:10: note: candidates are:
main.cpp:6:5: note: auto C::f(X&&) const & [with X = int&]
     f(X &&) const &
     ^
main.cpp:11:5: note: auto C::f(X&&) & [with X = int&]
     f(X &&) &
     ^
main.cpp:16:5: note: auto C::f(X&&) && [with X = int&]
     f(X &&) &&
     ^

But in case of #if 1 and #if 0, or #if 0 and #if 1 it compiles normally. Also if I replace all auto's with void's, then all compiles successfully too.

Is it bug, or just my misleading?

解决方案

g++ 4.8.2 has the same problem with the even simpler (Live at coliru):

struct A {
    auto f() & {}
    auto f() && {}
};

int main() {
    A{}.f();
    A a;
    a.f();
}

despite the program obviously being correct. It appears to be a bug in the interaction between ref-qualifiers and return type deduction: presumably the deduction process is stripping the qualifiers from the implicit object argument before handing them off to overload resolution.

I have reported this as GCC bug 60943.

这篇关于调用重载与ref限定符成员函数是不明确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:20