lundi 1 février 2021

Can't I convert std::reference

I would expect that std::reference_wrapper would work as a reference in terms of converting non-const into const, like:

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

The following code compiles and works fine in MSVC and GCC, but not on Clang. I just don't understand why, is it UB, or actually an issue on Clang compiler?

#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;
}

On Clang only, displays the following error:

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

Aucun commentaire:

Enregistrer un commentaire