首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用初始化程序列表初始化包含Boost.Fusion的boost::array<>向量

如何用初始化程序列表初始化包含Boost.Fusion的boost::array<>向量
EN

Stack Overflow用户
提问于 2022-07-01 00:25:00
回答 1查看 54关注 0票数 1

我需要使用初始化程序列表初始化一个boost::fusion::向量,其中包含一个boost::array作为元素。这个是可能的吗?

代码语言:javascript
复制
boost::array<bool, 2> ary{true, false}; // works
boost::fusion::vector<int, bool> vec1{ 5, false}; // works
boost::fusion::vector<bool, std::array<bool, 2>> vec2{ false, {true, false} }; // doesn't work :(

错误:

代码语言:javascript
复制
<source>: In function 'int main()':
<source>:116:55: error: converting to 'boost::fusion::vector<int, bool>' from initializer list would use explicit constructor 'boost::fusion::vector<T>::vector(U&& ...) [with U = {int, bool}; <template-parameter-2-2> = void; T = {int, bool}]'
     boost::fusion::vector<int, bool> vec1 = { 5, false};
                                                       ^
<source>:117:81: error: no matching function for call to 'boost::fusion::vector<bool, std::array<bool, 2> >::vector(<brace-enclosed initializer list>)'
     boost::fusion::vector<bool, std::array<bool, 2>> vec2{ false, {true, false} };
                                                                                 ^
In file included from /opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector.hpp:12,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/include/vector.hpp:11,
                 from <source>:14:
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:302:9: note: candidate: 'template<class Sequence, class> constexpr boost::fusion::vector<T>::vector(Sequence&&)'
         vector(Sequence&& seq)
         ^~~~~~
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:302:9: note:   template argument deduction/substitution failed:
<source>:117:81: note:   candidate expects 1 argument, 2 provided
     boost::fusion::vector<bool, std::array<bool, 2>> vec2{ false, {true, false} };
                                                                                 ^
In file included from /opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector.hpp:12,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/include/vector.hpp:11,
                 from <source>:14:
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:289:18: note: candidate: 'template<class ... U, class> boost::fusion::vector<T>::vector(U&& ...)'
         explicit vector(U&&... u)
                  ^~~~~~
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:289:18: note:   template argument deduction/substitution failed:
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:279:35: error: mismatched argument pack lengths while expanding 'boost::is_convertible<U, T>'
                 sizeof...(U) >= 1 &&
                 ~~~~~~~~~~~~~~~~~~^~
                 fusion::detail::and_<is_convertible<U, T>...>::value &&
                 ~~~~~~             
In file included from /opt/compiler-explorer/libs/boost_1_70_0/boost/config.hpp:61,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/aux_/config/msvc.hpp:19,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/aux_/config/adl.hpp:17,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/aux_/adl_barrier.hpp:17,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/bool_fwd.hpp:17,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/bool.hpp:17,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/aux_/na.hpp:17,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/mpl/vector.hpp:19,
                 from <source>:5:
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:274:9: note: candidate: 'constexpr boost::fusion::vector<T>::vector() [with T = {bool, std::array<bool, 2>}]'
         BOOST_DEFAULTED_FUNCTION(vector(), {})
         ^~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:274:9: note:   candidate expects 0 arguments, 2 provided
In file included from /opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector.hpp:12,
                 from /opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/include/vector.hpp:11,
                 from <source>:14:
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:262:12: note: candidate: 'constexpr boost::fusion::vector<bool, std::array<bool, 2> >::vector(const boost::fusion::vector<bool, std::array<bool, 2> >&)'
     struct vector
            ^~~~~~
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:262:12: note:   candidate expects 1 argument, 2 provided
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:262:12: note: candidate: 'constexpr boost::fusion::vector<bool, std::array<bool, 2> >::vector(boost::fusion::vector<bool, std::array<bool, 2> >&&)'
/opt/compiler-explorer/libs/boost_1_70_0/boost/fusion/container/vector/vector.hpp:262:12: note:   candidate expects 1 argument, 2 provided
<source>:115:27: warning: unused variable 'ary' [-Wunused-variable]
     boost::array<bool, 2> ary{true, false};
                           ^~~

使用GCC 8.3,助推1.70

我觉得我缺少对初始化程序列表工作方式的一些基本理解。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-07-01 10:58:00

这是对推导嵌套大括号初始化器的语言限制。

我能想到的使用C++17 CTAD的最佳版本是。

在编译器资源管理器上直播

代码语言:javascript
复制
#include <array>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/include/vector.hpp>

int main() {
  auto vec = boost::fusion::make_vector(false, std::array{true, false});
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72823262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档