Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法转换std::reference<;T>;至标准::参考<;常数T>;叮当声_C++_C++11_Visual C++_Clang_Std - Fatal编程技术网

C++ 无法转换std::reference<;T>;至标准::参考<;常数T>;叮当声

C++ 无法转换std::reference<;T>;至标准::参考<;常数T>;叮当声,c++,c++11,visual-c++,clang,std,C++,C++11,Visual C++,Clang,Std,我希望std::reference_包装器在将非常量转换为常量时可以用作引用,如: int a = 10; int& refA = a; const int& constRefA = refA; 下面的代码在MSVC和GCC中编译并运行良好,但在Clang中则不行。我只是不明白为什么,它是UB,还是实际上是关于Clang编译器的问题 #include <functional> #include <optional> int main() { i

我希望std::reference_包装器在将非常量转换为常量时可以用作引用,如:

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;
}
#包括
#包括
int main()
{
INTA=10;
std::reference_wrapper ref=a;
std::reference_wrapper constRef=ref;
std::可选optRef=a;
std::可选optConstRef=optRef;
返回0;
}
仅在叮当声上,显示以下错误:

prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
std::reference_wrapper<const int> constRef = ref;
prog.cc:13:39:错误:从“reference\u wrapper”到“reference\u wrapper”没有可行的转换
std::reference_wrapper constRef=ref;

prog.cc:13:39:错误:从“reference\u wrapper”到“reference\u wrapper”没有可行的转换
std::reference_wrapper constRef=ref;
^          ~~~
/opt/wandbox/clanghead/include/c++/v1/_ufunctional_base:374:28:注意:候选构造函数(隐式副本构造函数)不可行:第一个参数没有从“std::reference_wrapper”到“const std::reference_wrapper&”的已知转换
类_LIBCPP_模板_visreference_包装器
^
/opt/wandbox/clanghead/include/c++/v1/\uuuu functional\u base:374:28:注意:候选构造函数(隐式移动构造函数)不可行:第一个参数没有从“std::reference\u wrapper”到“std::reference\u wrapper&”的已知转换
/opt/wandbox/clanghead/include/c++/v1/uu functional_base:386:5:注意:候选构造函数不可行:第一个参数没有从“std::reference_wrapper”到“std::reference_wrapper::type&”(也称为“const int&”)的已知转换
引用\u包装(类型&\u\f)\u无例外
^
/opt/wandbox/clanghead/include/c++/v1/uu functional_base:389:14:注意:候选构造函数不可行:第一个参数没有从'std::reference_wrapper'到'std::reference_wrapper::type&'(也称为'const int&')的已知转换
专用:参考包装器(类型&&);public://=delete;//不要拘泥于临时工
^
/opt/wandbox/clanghead/include/c++/v1/_ufunctional_base:394:5:注意:候选函数
运算符类型&()const _NOEXCEPT{return*.\u f_;}
^
程序cc:16:54:错误:没有从“可选”到“可选”的可行转换
std::可选optConstRef=optRef;
^             ~~~~~~
/opt/wandbox/clanghead/include/c++/v1/optional:689:41:注意:候选构造函数不可行:没有已知的从第一个参数“std::optional”到“const std::optional&”的转换
_LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&)=默认值;
^
/opt/wandbox/clanghead/include/c++/v1/optional:690:41:注意:候选构造函数不可行:没有已知的从第一个参数的“std::optional”到“std::optional&&”的转换
_LIBCPP_INLINE_VISIBILITY constexpr可选(可选&&)=默认值;
^
/opt/wandbox/clanghead/include/c++/v1/optional:691:41:注意:候选构造函数不可行:第一个参数没有从“std::optional”到“std::nullopt_t”的已知转换
_LIBCPP_INLINE_VISIBILITY constexpr可选(nullopt_t)noexcept{}
^
/opt/wandbox/clanghead/include/c++/v1/optional:715:15:注意:已忽略候选模板:替换失败[with _Up=std::optional&]:std:_MetaBase中没有名为“_enableimpl”的成员
constexpr可选(_-Up&&u-v)
^
/opt/wandbox/clanghead/include/c++/v1/optional:730:5:注意:已忽略候选模板:替换失败[with _Up=std::reference_wrapper]:std:_MetaBase中没有名为“_enableimpl”的成员
可选(常量可选&_;v)
^
/opt/wandbox/clanghead/include/c++/v1/optional:748:5:注意:已忽略候选模板:替换失败[with _Up=std::reference_wrapper]:std:_MetaBase中没有名为“_enableimpl”的成员
可选(可选和&&uv)
^
/opt/wandbox/clanghead/include/c++/v1/optional:701:24:注意:显式构造函数不是候选构造函数
constexpr显式可选(_InPlaceT、_Args&&…u Args)
^
/opt/wandbox/clanghead/include/c++/v1/optional:722:24:注意:显式构造函数不是候选构造函数
constexpr显式可选(_-Up&&u-v)
^
/opt/wandbox/clanghead/include/c++/v1/optional:738:14:注意:显式构造函数不是候选构造函数
显式可选(常量可选&_v)
^
/opt/wandbox/clanghead/include/c++/v1/optional:756:14:注意:显式构造函数不是候选构造函数
显式可选(可选&&uv)
^
产生2个错误。

您在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;

重载解析是有效的,它适用于铿锵9及以上的
参考包装器

。谢谢@tedlynmo你说得对,在编译器资源管理器x64上它可以正常工作。然而,ARMV7版本clang显示了相同的错误。我不知道为什么它在wandbox上也不起作用,因为它是相同的编译器,奇怪的是,当使用在godbolt上工作的相同版本时,它在wandbox中不起作用。如果我使用Clang10.0.1在我的计算机上本地编译它,它也可以工作。这也是我的CI系统中正在发生的事情。Linux版本未编译。难道stdlib已经过时了吗?@alej可能。我找到了一个解决办法。
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;
 void FUN(int const&) {}
 FUN(ref)