当前位置: 首页 > 知识库问答 >
问题:

在Clang上无法将std::引用转换为std::引用

鲍鸿波
2023-03-14

我会期望std::reference_wrapper在将non-const转换为const方面可以作为参考,例如:

int a = 10;
int& refA = a;
const int& constRefA = refA;

下面的代码在MSVC和GCC中编译并运行良好,但在Clang上则不行。我只是不明白为什么,它是UB,还是实际上是关于Clang编译器的问题?

#include <functional>
#include <optional>

int main()
{
    int a = 10;

    std::reference_wrapper<int> ref = a;
    std::reference_wrapper<const int> constRef = ref;

    std::optional<std::reference_wrapper<int>> optRef = a;
    std::optional<std::reference_wrapper<const int>> optConstRef = optRef;

    return 0;
}

仅在Clang上,显示以下错误:

prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
std::reference_wrapper<const int> constRef = ref;

https://wandbox.org/permlink/FSY4tCvE9B17hbVn

prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
    std::reference_wrapper<const int> constRef = ref;
                                      ^          ~~~
/opt/wandbox/clang-head/include/c++/v1/__functional_base:374:28: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::reference_wrapper<int>' to 'const std::reference_wrapper<const int> &' for 1st argument
class _LIBCPP_TEMPLATE_VIS reference_wrapper
                           ^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:374:28: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int> &&' for 1st argument
/opt/wandbox/clang-head/include/c++/v1/__functional_base:386:5: note: candidate constructor not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int>::type &' (aka 'const int &') for 1st argument
    reference_wrapper(type& __f) _NOEXCEPT
    ^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:389:14: note: candidate constructor not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int>::type &&' (aka 'const int &&') for 1st argument
    private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
             ^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:394:5: note: candidate function
    operator type&() const _NOEXCEPT {return *__f_;}
    ^
prog.cc:16:54: error: no viable conversion from 'optional<reference_wrapper<int>>' to 'optional<reference_wrapper<const int>>'
    std::optional<std::reference_wrapper<const int>> optConstRef = optRef;
                                                     ^             ~~~~~~
/opt/wandbox/clang-head/include/c++/v1/optional:689:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'const std::optional<std::reference_wrapper<const int>> &' for 1st argument
    _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
                                        ^
/opt/wandbox/clang-head/include/c++/v1/optional:690:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'std::optional<std::reference_wrapper<const int>> &&' for 1st argument
    _LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
                                        ^
/opt/wandbox/clang-head/include/c++/v1/optional:691:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'std::nullopt_t' for 1st argument
    _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
                                        ^
/opt/wandbox/clang-head/include/c++/v1/optional:715:15: note: candidate template ignored: substitution failure [with _Up = std::optional<std::reference_wrapper<int>> &]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr optional(_Up&& __v)
              ^
/opt/wandbox/clang-head/include/c++/v1/optional:730:5: note: candidate template ignored: substitution failure [with _Up = std::reference_wrapper<int>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    optional(const optional<_Up>& __v)
    ^
/opt/wandbox/clang-head/include/c++/v1/optional:748:5: note: candidate template ignored: substitution failure [with _Up = std::reference_wrapper<int>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    optional(optional<_Up>&& __v)
    ^
/opt/wandbox/clang-head/include/c++/v1/optional:701:24: note: explicit constructor is not a candidate
    constexpr explicit optional(_InPlaceT, _Args&&... __args)
                       ^
/opt/wandbox/clang-head/include/c++/v1/optional:722:24: note: explicit constructor is not a candidate
    constexpr explicit optional(_Up&& __v)
                       ^
/opt/wandbox/clang-head/include/c++/v1/optional:738:14: note: explicit constructor is not a candidate
    explicit optional(const optional<_Up>& __v)
             ^
/opt/wandbox/clang-head/include/c++/v1/optional:756:14: note: explicit constructor is not a candidate
    explicit optional(optional<_Up>&& __v)
             ^
2 errors generated.

共有1个答案

施晗日
2023-03-14

您在wandbox上使用的std库存在错误。它只找到了2个构造函数和一个转换运算符。

编译器和标准库并不总是同步的。

printf("Hello World");
int a = 10;

std::reference_wrapper<int> ref = a;
std::reference_wrapper<const int> constRef( ref );

std::optional<std::reference_wrapper<int>> optRef = a;
std::optional<std::reference_wrapper<const int>> optConstRef( optRef );

return 0;

通过使转换显式化它可以工作。我不知道为什么;在我对reference_wrapper构造和转换运算符的阅读中没有任何显式转换差异。

但是,缺少隐式引用包装转换可以解释为什么可选项需要它。

无论如何,这显然是一个错误。的通用转换构造函数reference_wrapper

 void FUN(int const&) {}
 FUN(ref)

重载解析是有效的,对于reference\u包装器也是有效的

 类似资料: