首页 > 解决方案 > clang和gcc之间的右值引用重载区别

问题描述

我正在实现一个 map 函数,有两个重载:一个用于右值,另一个用于 const 引用:

template <class Array, class UnaryOp>
void map_inplace(UnaryOp op, Array &a) {
    std::transform(a.cbegin(), a.cend(), a.begin(), op);
}

template <class Array, class UnaryOp>
auto map(UnaryOp op, Array &&a) {
    map_inplace(op, a);
    return a;
}

template <class Array, class UnaryOp>
auto map(UnaryOp op, const Array &a) {
    Array res(a);
    map_inplace(op, res);
    return res;
}

我有以下测试:

TEST_CASE("map") {
    const std::vector v{1., 2., 3.};

    // I do expect the const reference overload to be called
    REQUIRE(almost_equal(map(std::negate<>(), v), {-1., -2., -3.}));

    // Check const vector is not modified
    REQUIRE(v == std::vector{1., 2., 3.});
}

用 Clang 运行它测试通过:

通过:几乎等于(map(std::negate<>(), v), {-1., -2., -3.}) 为:true

通过: v == std::vector{1., 2., 3.} 对于: { 1.0, 2.0, 3.0 } == { 1.0, 2.0, 3.0 }

通过 1 个带有 2 个断言的测试用例。

但是 GCC 失败了:

通过:几乎等于(map(std::negate<>(), v), {-1., -2., -3.}) 为:true

失败:v == std::vector{1., 2., 3.} 对于:{ -1.0, -2.0, -3.0 } == { 1.0, 2.0, 3.0 }

1 个测试用例失败,1 个断言失败。

因此 GCC 正在调用右值引用重载并正在修改 const 定义的向量。

GCC 版本是:

gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0

Clang 版本是:

clang 版本 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

编辑:

所以我尝试了更多测试,但 GCC 失败了:

TEST_CASE("map") {
    const std::vector v{1., 2., 3.};
    REQUIRE((map(std::negate<>(), v) == std::vector{-1., -2., -3.}));
    REQUIRE(v == std::vector{1., 2., 3.});
}

但是如果我添加模板类型参数:

TEST_CASE("map") {
    const std::vector<double> v{1., 2., 3.};
    REQUIRE((map(std::negate<>(), v) == std::vector<double>{-1., -2., -3.}));
    REQUIRE(v == std::vector<double>{1., 2., 3.});
}

那么它的工作原理!

标签: c++gccclangoverloadingrvalue-reference

解决方案


演示此问题的最小示例:

#include <vector>
#include <type_traits>

const ::std::vector<double> v1{1., 2., 3.};
const ::std::vector v{1., 2., 3.};
static_assert(::std::is_same_v<decltype(v1), decltype(v)>); // error on gcc

在线编译器

错误 80990 - 使用类模板参数推导在变量定义中忽略 cv 限定符


推荐阅读