c++ - 为什么这种使用 boost::none 无法使用 nvcc 编译?
问题描述
我正在尝试编译以下代码:
#include <boost/optional.hpp>
void foo(boost::optional<unsigned> x = boost::none);
使用以下命令行放置在文件a.cu
中,使用 CUDA 编译器:
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。
推荐阅读
- python - 在excel或python中用已知的两个值填充缺失的中间值
- java - Gradle 升级但 gradle -v 仍然显示旧版本 - macOS
- amazon-web-services - 在 AWS DL AMI 上支持 GPU 的 docker image xgboost
- javascript - React 制作注册复选框
- javascript - 如何在 Next.js 中渲染页面之前重定向?
- python - 两个空格之间的熊猫字符
- mongodb - 如何在 Luminus 应用程序中检查 Monger 连接(MongoDB 不可用)?
- javascript - WKWebView 和 KeyChain
- github - 如何将代码片段嵌入对话中?
- python - 为什么我不能在使用循环时制作一排不同颜色的矩形?