[Boost-users] [fusion] Adapted struct I/O

150 views
Skip to first unread message

Mateusz Łoskot

unread,
Nov 2, 2011, 6:56:27 PM11/2/11
to boost...@lists.boost.org
Hi,

I'm still taking early steps with Boost.Fusion.
I'm trying to adapt a bunch of struct and template struct for easy I/O
operations.
I don't understand one thing from the docs. Here we go;

1) Given the example [1] of template struct adoption:

namespace demo
{
template<typename Name, typename Age>
struct employee
{
Name name;
Age age;
};
}

// Any instantiated demo::employee is now a Fusion sequence
BOOST_FUSION_ADAPT_TPL_STRUCT(
(Name)(Age),
(demo::employee) (Name)(Age),
(Name, name)
(Age, age))

[1] http://www.boost.org/doc/libs/1_47_0/libs/fusion/doc/html/fusion/adapted/adapt_tpl_struct.html

2) The included comment says "demo::employee is now a Fusion sequence".

3) Jumping to I/O and out docs, reading that [2]

"The I/O operators: << and >> work generically on all Fusion sequences. "

[2] http://www.boost.org/doc/libs/1_47_0/libs/fusion/doc/html/fusion/sequence/operator/i_o.html

4) Means, I should be able to stream Fusion-adopted demo::employee

#include <iostream>
#include <string>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/include/io.hpp>

// code from above example goes here

int main()
{
demo::employee<std::string, int> e;
std::cout << e << std::endl;
}

5) Given what I have learned above, I expect the cout << e to work:

$ g++ -I/home/mloskot/dev/boost/_svn/trunk boost_fusion.cpp
boost_fusion.cpp: In function ‘int main()’:
boost_fusion.cpp:28:18: error: no match for ‘operator<<’ in ‘std::cout << e’
...
I assume complete error is not necessary as my question is of general nature.

Is my expectation valid?

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
Member of ACCU, http://accu.org
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Vicente J. Botet Escriba

unread,
Nov 2, 2011, 8:41:04 PM11/2/11
to boost...@lists.boost.org

Le 02/11/11 23:56, Mateusz Łoskot a écrit :
Hi,

try adding

#include <boost/fusion/include/io.hpp>

It is at the end of

[2] http://www.boost.org/doc/libs/1_47_0/libs/fusion/doc/html/fusion/sequence/operator/i_o.html


Best,
Vicente

Mateusz Łoskot

unread,
Nov 2, 2011, 8:55:08 PM11/2/11
to boost...@lists.boost.org
On 3 November 2011 00:41, Vicente J. Botet Escriba

<vicent...@wanadoo.fr> wrote:
> Le 02/11/11 23:56, Mateusz Łoskot a écrit :
> >
> > 4) Means, I should be able to stream Fusion-adopted demo::employee
> >
> > #include <iostream>
> > #include <string>
> > #include <boost/fusion/adapted/struct/adapt_struct.hpp>
> > #include <boost/fusion/include/adapt_struct.hpp>
> > #include <boost/fusion/sequence/io.hpp>
> > #include <boost/fusion/include/io.hpp>
> >
> > // code from above example goes here
>
> Hi,
>
> try adding
>
> #include <boost/fusion/include/io.hpp>
>
> It is at the end of

Hi,

It is there. See my example above.

BTW, I should have mentioned I use Boost trunk with GCC 4.5.2 and
Visual C++ 2010

Nathan Crookston

unread,
Nov 3, 2011, 2:46:08 AM11/3/11
to boost...@lists.boost.org
Hi Mateusz,

2011/11/2 Mateusz Łoskot <mat...@loskot.net>:


> On 3 November 2011 00:41, Vicente J. Botet Escriba
> <vicent...@wanadoo.fr> wrote:
>> try adding
>>
>> #include <boost/fusion/include/io.hpp>
>>
>> It is at the end of
>
> Hi,
>
> It is there. See my example above.

While io.hpp includes the appropriate operator<<, it defines it in
namespace fusion and fusion::operators. Since the code generated from
struct adaptation appears to reside in fusion::traits and
fusion::extension, I'm guessing that adapted structs don't have ADL
kicking in. Placing a 'using boost::fusion::operators::operator<<;'
declaration in namespace demo is one way to fix the problem.

HTH,
Nate

Mateusz Łoskot

unread,
Nov 3, 2011, 7:19:26 AM11/3/11
to boost...@lists.boost.org
Hi Nathan,

2011/11/3 Nathan Crookston <nathan.c...@gmail.com>:


> 2011/11/2 Mateusz Łoskot <mat...@loskot.net>:
>> On 3 November 2011 00:41, Vicente J. Botet Escriba
>> <vicent...@wanadoo.fr> wrote:
>>> try adding
>>>
>>> #include <boost/fusion/include/io.hpp>
>>>
>>> It is at the end of
>>
>>

>> It is there. See my example above.
>
> While io.hpp includes the appropriate operator<<, it defines it in
> namespace fusion and fusion::operators.  Since the code generated from
> struct adaptation appears to reside in fusion::traits and
> fusion::extension, I'm guessing that adapted structs don't have ADL
> kicking in.  Placing a 'using boost::fusion::operators::operator<<;'
> declaration in namespace demo is one way to fix the problem.

Spot on. Works. Thanks!

Not sure if I missed it while reading the docs, or the docs
don't mention this trick.

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
Member of ACCU, http://accu.org

Vicente J. Botet Escriba

unread,
Nov 3, 2011, 1:10:14 PM11/3/11
to boost...@lists.boost.org
Le 03/11/11 01:55, Mateusz Łoskot a écrit :

> On 3 November 2011 00:41, Vicente J. Botet Escriba
> <vicent...@wanadoo.fr> wrote:
>> Le 02/11/11 23:56, Mateusz Łoskot a écrit :
>>> 4) Means, I should be able to stream Fusion-adopted demo::employee
>>>
>>> #include<iostream>
>>> #include<string>
>>> #include<boost/fusion/adapted/struct/adapt_struct.hpp>
>>> #include<boost/fusion/include/adapt_struct.hpp>
>>> #include<boost/fusion/sequence/io.hpp>
>>> #include<boost/fusion/include/io.hpp>
>>>
>>> // code from above example goes here
>> Hi,
>>
>> try adding
>>
>> #include<boost/fusion/include/io.hpp>
>>
>> It is at the end of
> Hi,
>
> It is there. See my example above.
>
Sorry, I should to sleep earlier ;-)

Best,
Vicente

Reply all
Reply to author
Forward
0 new messages