我正在尝试编译以下代码:
#include <boost/optional.hpp>
void foo(boost::optional<unsigned> x = boost::none);
使用 CUDA 编译器,使用以下命令行放置在文件 a.cu
中:
nvcc a.cu -c --std=c++11 -I/opt/boost/include
但是我收到了一堆错误:
a.cu:2:53: error: conversion from ‘const boost::none_t(boost::none_t::init_tag (*)())’ to ‘boost::optional<unsigned int>’ is ambiguous
void foo(boost::optional<unsigned> x = boost::none);
^
/opt/boost/include/boost/optional/optional.hpp:805:1: note: candidate: boost::optional<T>::optional(boost::optional<T>::rval_reference_type) [with T = unsigned int; boost::optional<T>::rval_reference_type = unsigned int&&] <near match>
optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
^ ~~~~
/opt/boost/include/boost/optional/optional.hpp:805:1: note: conversion of argument 1 would be ill-formed:
a.cu:2:53: error: invalid conversion from ‘const boost::none_t (*)(boost::none_t::init_tag (*)())’ to ‘unsigned int’ [-fpermissive]
void foo(boost::optional<unsigned> x = boost::none);
^
/opt/boost/include/boost/optional/optional.hpp:800:1: note: candidate: boost::optional<T>::optional(boost::optional<T>::argument_type) [with T = unsigned int; boost::optional<T>::argument_type = const unsigned int&] <near match>
optional ( argument_type val ) : base(val) {}
^ ~~~~
/opt/boost/include/boost/optional/optional.hpp:800:1: note: conversion of argument 1 would be ill-formed:
a.cu:2:53: error: invalid conversion from ‘const boost::none_t (*)(boost::none_t::init_tag (*)())’ to ‘unsigned int’ [-fpermissive]
void foo(boost::optional<unsigned> x = boost::none);
为什么会发生这种情况?我可以在使用 nvcc 编译的(主机端)代码中实际使用 boost::Optional 的同时规避该问题吗?
其他信息:
- 该代码可以使用 g++ 6.3.0(我的发行版的编译器)正常编译。
- 这段代码(或者更确切地说,类似的代码)用于在我使用的早期 Linux 发行版上进行编译和工作,其中编译器是 g++ 5.4.x 。
- 我已在 Boost 版本 1.65.1 和 1.69.0 上尝试过此操作。
- 我已经使用 CUDA 版本 9.2.88 和 10.0.130 尝试过此操作。
最佳答案
我遇到了完全相同的错误,并且能够通过此修改使其正常工作:
#define BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
#include <boost/optional.hpp>
这里使用 CUDA 10.0.130、g++ 7.3.0 和 Boost 1.68.0。
关于c++ - 为什么使用 boost::none 无法通过 nvcc 编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611222/