简体   繁体   English

不同版本的 g++ 重载解析结果不一致

[英]Different versions of g++ have inconsistent result of overload resolution

When I used g++ 5.4.0, the sample code below worked as expected, but after I updated the g++ to 10.2.0, the result was changed.当我使用 g++ 5.4.0 时,下面的示例代码按预期工作,但在我将 g++ 更新为 10.2.0 后,结果发生了变化。 I also tested the sample code on clang++ 11.0.1, and the result was the same as g++ 5.4.0.我也在clang++ 11.0.1上测试了示例代码,结果和g++ 5.4.0一样。

I have searched some relevant questions but did not get a valid answer.我搜索了一些相关的问题,但没有得到有效的答案。 As I know, the overload function should be matched before the template, Why does g++ 10.2.0 get a different result, and how can I resolve this?据我所知,重载函数应该在模板之前匹配,为什么g++ 10.2.0得到不同的结果,我该如何解决?

Because the original source codes are very complex, so it is not easy to refactor them with other c++ features, could this problem be fixed with a smaller change?因为原始源代码非常复杂,所以用其他c++特性重构它们并不容易,这个问题可以通过较小的改动来解决吗?

The target of the sample code is using the overload function Base::operator const std::string&() to execute some special action and using the template function to execute common action.示例代码的目标是使用重载函数Base::operator const std::string&()执行一些特殊操作,并使用模板函数执行常见操作。

#include <string>
#include <iostream>

class Base 
{
public:
    template <class T>
    operator const T&() const;
    virtual operator const std::string&() const;
};

template <class T>
Base::operator const T&() const
{
    std::cout << "use template method" << std::endl;
    static T tmp{};
    return tmp;
}

Base::operator const std::string&() const
{
    std::cout << "use overload method" << std::endl;
    const static std::string tmp;
    return tmp;
}

template <class T>
class Derive : public Base
{
public:
    operator const T&() const
    {
        const T& res = Base::operator const T&();
        return res;
    }
};

int main()
{
    Derive<std::string> a;
    const std::string& b = a;
    return 1;
}

The g++ 5.4.0 result: g++ 5.4.0 结果:

g++ -std=c++11  main.cpp -o test && ./test
use overload method

The g++ 10.2.0 result: g++ 10.2.0 结果:

g++ -std=c++11 main.cpp -o test && ./test          
use template method

The clang++ 11.0.1 result: clang++ 11.0.1 结果:

clang++ -std=c++11  main.cpp -o test && ./test
use overload method

This is definitely a GCC bug:这绝对是一个 GCC 错误:

template <class T>
class Derive : public Base {
 public:
  operator const T&() const override {
    using Y = std::string;
    static_assert(std::is_same<T, Y>::value, "");
    
    std::string static res;

    res = Base::operator const Y&();
    res = Base::operator const T&();
    return res;
  }
};

Here, 2 different versions of the operator are called even though Y and T are identical.这里,即使YT相同,也会调用 2 个不同版本的运算符。 Godbolt神箭

Clang has the correct behavior, you can use that as a workaround. Clang 具有正确的行为,您可以将其用作解决方法。 Do report the bug so it can be fixed in following releases of GCC.请报告错误,以便它可以在 GCC 的后续版本中修复。

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

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