Boost.Fusion define struct array member
Boost.Fusion define struct array member
你是怎么做到的?
using char10 = char[10];
BOOST_FUSION_DEFINE_STRUCT(
(demo), employee,
(char10, name))
不起作用:
主.cpp:8:1:错误:将"boost::call_traits::p aram_type {aka const char* const}"赋值到"char10 {aka char [10]}"中的不兼容类型
using char10 = char[10];
BOOST_FUSION_DEFINE_STRUCT(
(demo), employee,
(decltype(char10{}), name))
也不起作用:
主.cpp:8:1:错误:引用类型"char (&([10]"的值初始化
这对于 C
风格的数组是不可能的,因为 C 具有臭名昭著的数组到指针衰减属性(并且仍然绑定C++以实现向后兼容性(。
这会中断,因为 Fusion 宏会生成如下代码:
namespace demo {
struct employee {
typedef employee self_type;
char10 name;
employee() : name() {}
employee(self_type const &) = default;
employee(self_type &&) = default;
template <typename Seq>
employee(Seq const &seq, typename boost::disable_if<boost::is_convertible<Seq const &, char10> >::type * = 0)
: name(boost::fusion::deref(boost::fusion::advance_c<0>(boost::fusion::begin(seq)))) {}
self_type &operator=(self_type const &) = default;
self_type &operator=(self_type &&) = default;
template <typename Seq> self_type &operator=(Seq const &seq) {
typedef typename boost::fusion::result_of::begin<Seq const>::type I0;
I0 i0 = boost::fusion::begin(seq);
name = boost::fusion::deref(i0);
return *this;
}
explicit employee(boost::call_traits<char10>::param_type arg) : name(arg) {}
};
} // namespace demo
在构造函数的初始值设定项列表中:
explicit employee(boost::call_traits<char10>::param_type arg) : name(arg) {}
arg
的类型将char const*
这不是字符串的有效初始值设定项(char const(&)[10]
将是,但需要
溶液
走C++路:
Live On Coliru (c++11(
#include <boost/fusion/include/define_struct.hpp>
using char10 = std::array<char, 10>;
BOOST_FUSION_DEFINE_STRUCT(
(demo), employee,
(char10, name))
#include <boost/core/demangle.hpp>
#include <iostream>
int main() {
demo::employee emp;
emp.name = {{'I',' ','a','m',' ','G','r','o','o','t'}};
}
如果您被困在黑暗时代,您可以使用boost::array
:
Live On Coliru (c++03(
相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- OpenGL VBO Indexing ( How to compute Index Array)
- 标准::unordered_map 中的 std::array 的值初始化
- 收到错误"invalid use of non-static data member 'stu::n' "
- "Warning: Comma within array index expression"但逗号分隔函数参数
- Centos7 g++ "to_string is not in a member of std"
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- 将 **float array 从 C++ Dll 传递给 python
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 我可以安全地复制矢量<array>吗?
- Boost.Fusion define struct array member
- VC++ 2010 "array is not a member of std"错误