0

上一个问题:std::string 类继承和繁琐的 c++ 重载解析

在上一个问题之后的步骤中,我尝试测试operator+原始字符串指针:"aaa" + path_string{ "bbb" }. 并发现它没有调用相应的path_string类友函数。

我试图添加不模板重载operator+ (2),但它也没有工作。但我发现模板化的(3)确实有效。

#include <string>

template <class t_elem, class t_traits, class t_alloc>
class path_basic_string : public std::basic_string<t_elem, t_traits, t_alloc>
{
public:
    using base_type = std::basic_string<t_elem, t_traits, t_alloc>;

    path_basic_string() = default;
    path_basic_string(const path_basic_string & ) = default;
    path_basic_string(path_basic_string &&) = default;

    path_basic_string & operator =(path_basic_string path_str)
    {
        this->base_type::operator=(std::move(path_str));
        return *this;
    }

    path_basic_string(base_type r) :
        base_type(std::move(r))
    {
    }

    path_basic_string(const t_elem * p) :
        base_type(p)
    {
    }

    base_type & str()
    {
        return *this;
    }

    const base_type & str() const
    {
        return *this;
    }

    using base_type::base_type;
    using base_type::operator=;

    // ... all over operators are removed as not related to the issue ...

    // (1)
    friend path_basic_string operator+ (const t_elem * p, const base_type & r)
    {
        path_basic_string l_path = p;
        l_path += "xxx";
        return std::move(l_path);
    }

    friend path_basic_string operator+ (const t_elem * p, base_type && r)
    {
        if (!r.empty()) {
            return "111" + ("/" + r); // call base operator instead in case if it is specialized for this
        }

        return "111";
    }

    // (2)
    friend path_basic_string operator+ (const t_elem * p, path_basic_string && r)
    {
        base_type && r_path = std::move(std::forward<base_type>(r));
        if (!r_path.empty()) {
            return "222" + ("/" + r_path); // call base operator instead in case if it is specialized for this
        }

        return "222";
    }

    // (3) required here to intercept the second argument
    template <typename T>
    friend path_basic_string operator+ (const t_elem * p, T && r)
    {
        base_type && r_path = std::move(std::forward<base_type>(r));
        if (!r_path.empty()) {
            return "333" + ("/" + r_path); // call base operator instead in case if it is specialized for this
        }

        return "333";
    }
};

using path_string = path_basic_string<char, std::char_traits<char>, std::allocator<char> >;

std::string test_path_string_operator_plus_right_xref(path_string && right_path_str)
{
    return "aaa" + right_path_str;
}

int main()
{
    const path_string test =
        test_path_string_operator_plus_right_xref(std::move(path_string{ "bbb" }));
    printf("-%s-\n", test.str().c_str());

    return 0;
}

3 个编译器的输出:gcc 5.4、clang 3.8.0、msvc 2015 (19.00.23506)

-333/bbb-

https://rextester.com/BOFUS59590

正如我所记得的,C++ 标准澄清了这一点,因为只有当没有一个非模板化函数与参数完全匹配时,才需要查找模板化函数。但是(2)操作符必须完全匹配,但是为什么它甚至没有被调用呢?

如果 remove(3)则将(1)调用而不是(2)which is match than (1)

这里发生了什么?

PS:我认为这与上一个问题中的const+single reference类似。

4

1 回答 1

2

在以下片段中:

std::string test_path_string_operator_plus_right_xref(path_string && right_path_str)
{
    return "aaa" + right_path_str;
}

right_path_str是一个非常量左值。因此,它永远不能绑定到右值引用。

当模板重载不可用时,它会绑定到 const 左值引用重载

friend path_basic_string operator+ (const t_elem * p, const base_type & r)

当模板存在时,它更适合非常量左值引用:

template <typename T>
friend path_basic_string operator+ (const t_elem * p, T && r)

在这种情况下,T&&是一个转发引用,它折叠为非常量左值引用。要修复您的代码,请确保move在调用函数时使用右值引用参数并养成习惯 - 每当您在下面传递右值引用时,使用std::move,并且每当您传递转发引用时,使用std::forward.

std::string test_path_string_operator_plus_right_xref(path_string && right_path_str)
{
    return "aaa" + std::move(right_path_str);
}
于 2018-11-06T17:11:21.850 回答