首页 > 解决方案 > 为什么这种使用 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 的同时规避这个问题吗?

附加信息:

标签: c++cudaoptionalcompiler-bugboost-optional

解决方案


我遇到了完全相同的错误,并且能够使其与此修改一起使用:

    #define BOOST_OPTIONAL_USE_OLD_DEFINITION_OF_NONE
    #include <boost/optional.hpp>

这是使用 CUDA 10.0.130、g++ 7.3.0 和 Boost 1.68.0。


推荐阅读