Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Posted on 2013-06-02
Medium Priority
674 Views
Given:

``````#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/vector.hpp>

#include <boost/fusion/mpl.hpp>
#include <boost/array.hpp>
// boost::fusion::result_of::value_at
#include <boost/fusion/sequence/intrinsic/value_at.hpp>
#include <boost/fusion/include/value_at.hpp>

// boost::fusion::result_of::size
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/include/size.hpp>

// boost::fusion::at
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/at.hpp>

# include <boost/bind.hpp>

# include <iostream>
# include <vector>
# include <typeinfo>
# include <cstdio>

typedef std::vector < int > INT_VEC ;
typedef boost::array<int, 10> array_t;
struct my_struct {
int          i;
bool         j;
array_t      arr1;
unsigned int arr2 [ 10 ];
INT_VEC      arr3 ;
my_struct()
: i    ( 0x4000 )
, j    ( false )
, arr3 ( 10, 6 )
{
for ( unsigned int odx ( 0 ); odx < 10; ++odx ) {
arr2 [ odx ] = odx + 15  ;
}
}
};

// Generate an adapter allowing to view "Foo" as a Boost.Fusion sequence
my_struct,
(int, i)
(bool, j)
(array_t, arr1 )
(unsigned int, arr2 [ 10 ] )
(INT_VEC, arr3)
)

template <typename T2> struct parse {
static void decode(  T2   & f);
};

struct work {
template <typename T>
void operator()(  T& t) const {
parse<T>::decode(t);
}
};

template <typename T2>
void parse<T2>::decode(  T2 & f) {
boost::fusion::for_each(f, work());
};

template <> void parse< unsigned int >::decode(  unsigned int  & f) {
std::cout << f << std::endl;
}

template <> void parse<int >::decode(  int  & f) {
std::cout << f << std::endl;
}
template <> void parse<char>::decode(  char & f) {
std::cout << f << std::endl;
}
template <> void parse<bool>::decode(  bool&  f) {
if ( f ) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
}
template <> void parse<double>::decode(  double&  f) {
std::cout << "(double = " << f << std::endl;
}

template <> void parse<array_t>::decode(   array_t&  f)  {
for(int i = 0; i < 10; ++i)
std::cout << f[i] << ";" ;
std::cout << std::endl;
}

template <> void parse<INT_VEC>::decode(   INT_VEC&  f)  {
for(int i = 0; i < 10; ++i)
std::cout << f[i] << ";" ;
std::cout << std::endl;
}

int main() {
my_struct obj ;
for ( unsigned int odx ( 0 ); odx < 10; ++odx ) {
obj.arr1 [ odx ] = odx ;
}
parse<my_struct>::decode( obj );
}
``````

a)
How do I specialize the C style array (arr2) such that I could iterate over the array and display the contents?

b)

``````template <> void parse<INT_VEC>::decode(   INT_VEC&  f)  {
for(int i = 0; i < 10; ++i)
std::cout << f[i] << ";" ;
std::cout << std::endl;
}
``````

f.size() does not produce the correct number of elements (10).   Why?
0
Question by:forums_mp
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

LVL 35

Accepted Solution

sarabande earned 800 total points
ID: 39218453
``````(unsigned int, arr2 [ 10 ] )
``````

the current definition wasn't for an array but for a single unsigned int. and the address was out-of-boundary.

in my opinion you need an array type. you could try

``````typedef unsigned int UINTARR10[10];
....
my_struct,
....
(UINTARR10, arr2),
....
``````

if that  doesn't compile, it is probably because of a missing operator. you then could define a structure that contains the array and provides the missing properties.

Sara
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month12 days, 3 hours left to enroll