template and boost::tuples
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: template and boost::tuples

  1. #1
    Join Date
    Jun 2011
    Posts
    3

    template and boost::tuples

    The code is as follows:

    #include "boost/tuple/tuple.hpp"

    template< typename iter1_T = boost::tuples::null_type,
    typename iter2_T = boost::tuples::null_type,
    typename iter3_T = boost::tuples::null_type,
    typename iter4_T = boost::tuples::null_type,
    typename iter5_T = boost::tuples::null_type,
    typename iter6_T = boost::tuples::null_type,
    typename iter7_T = boost::tuples::null_type,
    typename iter8_T = boost::tuples::null_type,
    typename iter9_T = boost::tuples::null_type,
    typename iter10_T = boost::tuples::null_type
    >
    struct tuples_N {
    typedef boost::tuple<iter1_T&, iter2_T&, iter3_T&, iter4_T&, iter5_T&, iter6_T&, iter7_T&, iter8_T&, iter9_T&, iter10_T&> tuple_N;

    };

    int main() {
    int *x;
    int *y;
    int *z;

    tuples_N<int*,int*,int*>::tuple_N *pp = new tuples_N<int*,int*, int*>::tuple_N(x,y,z);
    }

    Got a compilation warning, though the compilation went thru. The warning is rather long.
    You will see it if you compile it with g++:
    g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)

    In essence, the warning is about
    /usr/include/boost/tuple/detail/tuple_basic.hpp:447: warning: default-initialization of 'boost::tuples::null_type& boost::tuples::cons<boost::tuples::null_type&, boost::tuples::null_type>::head', which has reference type

    I'm wondering if there's a way to get rid of the warning by changing the code in main() as
    I can't change the struct. I understand that if I change iter1_T& to iter1_T in the struct, then the warning goes away.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,011

    Re: template and boost::tuples

    Quote Originally Posted by twinlove View Post
    I'm wondering if there's a way to get rid of the warning by changing the code in main() as
    I can't change the struct. I understand that if I change iter1_T& to iter1_T in the struct, then the warning goes away.
    What's the purpose of tuples_N? What are you trying to do?

    I don't see much use of storing references in a tuple. It's easier to store values.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: template and boost::tuples

    Quote Originally Posted by twinlove View Post
    I'm wondering if there's a way to get rid of the warning by changing the code in main() as
    I think the problem is that tuple_N defines the tuple to hold 10 reference types and that references, unlike pointers, never can be null. This means you'll have to supply a value to all 10 parameters when an object is instantiated, like,
    Code:
    boost::tuples::null_type n; // dummy variable to supply reference
    
    tuples_N<int*,int*,int*>::tuple_N *pp = 
       new tuples_N<int*,int*,int*>::tuple_N(x,y,z,n,n,n,n,n,n,n); // all 10 parameters supplied
    This takes care of the error/warning but is unsatisfactory of course since the tuple now holds 10 parameters instead of the wanted three (the type is okay but the object is wasteful).

    To fix this flaw I suppose a more "advanced" tuples_N template must be introduced. In the meantime you could use boost:tuple directly like,
    Code:
    boost::tuple<int*&,int*&,int*&> *pp = new boost::tuple<int*&,int*&,int*&>(x,y,z);
    Last edited by nuzzle; June 15th, 2011 at 09:55 AM.

  4. #4
    Join Date
    Jun 2011
    Posts
    3

    Re: template and boost::tuples

    Thank you for your help.

  5. #5
    Join Date
    Jun 2011
    Posts
    3

    Re: template and boost::tuples

    Thanks a lot for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center