|
Boost Users : |
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2008-05-21 12:17:09
Hansi wrote:
>
> Noah Roberts schrieb:
>> Hansi wrote:
>>> Hello,
>>>
>>> at the moment I want to make a getter function for a tuple type. The
>>> tuple is internally hidden in a class. For that I want to make a
>>> function which returns the values inside the tuple. The best solution
>>> would be if I can make a enum which defines the position inside the
>>> tuple and a template function which returns the value for this tuple.
>>>
>>> I have tested the following, but it doesn't work:
>>>
>>> typedef struct Members
>>> {
>>> enum Member
>>> {
>>> name = 0,
>>> value = 1,
>>> };
>>> }Members;
>>>
>>> template<enum Member member>
>>> element<0, Properties::Property>::type name()(const
>>> boost::tuples::tuple<std::wstring, boost::any>& prop)
>>> {
>>> return boost::tuples::get<member>(prop);
>>> }
>> I didn't quite understand your goals here so I implemented both I
>> thought you could mean:
>>
>>
>> #include <boost/tuple/tuple.hpp>
>> #include <string>
>> #include <iostream>
>>
>> template < typename T1, typename T2 >
>> struct Test
>> {
>> typedef boost::tuple<T1,T2> tuple_t;
>> tuple_t tup;
>>
>> enum Members { NAME, VALUE };
>>
>> typename boost::tuples::element<NAME, tuple_t>::type name() { return
>> tup.get<NAME>(); }
>
> this version doesn't work with my compiler (msvc8.0). I get the error:
>
> error C2899:typename cannot be used outside a template declaration
>
> But this would be the preferred version for me. Have you an idea how I
> can solve this?
That's exactly the compiler I used to compile this code. Have you
altered it in some way? The typename keyword is quite specifically
needed in the above code, but would be quite specifically disallowed
with some minor differences, namely if the "Test" class was not
templated or if the tuple type was not dependent on any of the template
parameters.
Try to just create a new project and copy/paste my code into it,
replacing their non-standard main function with the one I have. You'll
probably need to keep the "stdafx.h" include.
What version of boost? I have 1.34 on this system; we have not upgraded
yet as our products will adversely affected by changes to some libraries
and we're not yet prepared to deal with that.
I have a feeling though that you tried to integrate my code into your
particular problem without first testing mine. You may very well not
need the 'typename' keyword. I would suggest really studying when and
where to use that keyword if that's the case for it is fundamental to
understanding templates and is not the easiest thing in the world to learn.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net