问题描述
此代码被拒绝:
const std::optional<int> opt{5};
std::optional<volatile int> opt2 = opt;
但这是可以接受的:
const std::optional<int> opt{5};
std::optional<const int> opt2 = opt;
这是怎么回事?
In file included from /tmp/a.cpp:1: /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/optional:627:4: error: no matching function for call to 'operator new' ::new ^~ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/optional:1140:10: note: in instantiation of function template specialization 'std::_Optional_base_impl >::_M_construct' requested here this->_M_construct(std::forward(__args)...); ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/optional:1002:4: note: in instantiation of function template specialization 'std::optional::emplace' requested here emplace(*__t); ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/new:134:7: note: candidate function not viable: no known conversion from 'volatile int *' to 'const std::nothrow_t' for 2nd argument void* operator new(std::size_t,const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/new:143:7: note: candidate function not viable: no known conversion from 'volatile int *' to 'std::align_val_t' for 2nd argument void* operator new(std::size_t,std::align_val_t) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/new:168:14: note: candidate function not viable: no known conversion from 'volatile int *' to 'void *' for 2nd argument inline void* operator new(std::size_t,void* __p) _GLIBCXX_USE_NOEXCEPT ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/new:120:7: note: candidate function not viable: requires 1 argument,but 2 were provided void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/new:145:7: note: candidate function not viable: requires 3 arguments,but 2 were provided void* operator new(std::size_t,std::align_val_t,const std::nothrow_t&) ^ 1 error generated.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)