boost tuple question

This is a discussion on boost tuple question within the C++ Programming forums, part of the General Programming Boards category; The following code: Code: boost::tuple<int, std::string, double> t; int i = 0; boost::tuples::get<i>(t) = 1; i++; boost::tuples::get<i>(t) = "foo"; i++; ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144

    boost tuple question

    The following code:
    Code:
    boost::tuple<int, std::string, double> t;
    int i = 0;
    boost::tuples::get<i>(t) = 1;
    i++;
    boost::tuples::get<i>(t) = "foo";
    i++;
    boost::tuples::get<i>(t) = 3.1416;
    gives the following compiler errors on gcc 4.2.1:

    Code:
    error: 'i' cannot appear in a constant-expression
    error: no matching function for call to 'get(boost::tuples::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)'
    error: 'i' cannot appear in a constant-expression
    error: no matching function for call to 'get(boost::tuples::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)'
    error: 'i' cannot appear in a constant-expression
    error: no matching function for call to 'get(boost::tuples::tuple<int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>&)'
    is there a good reason why the template parameter for choosing the element has to be a constant expression? doesn't that limit the usefulness of the tuple type?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Non-type template parameters have to be compile-time constants as per the language rules. Otherwise, how could the compiler instantiate the template?

    And there is a very good reason for the parameter of tuples::get being a template (and thus compile-time) parameter. What would the return type of the function be otherwise?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost multi_array question
    By Raptoricus in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2009, 03:22 AM
  2. building boost iostreams
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2007, 02:29 PM
  3. Integrating Boost with STLPort
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-11-2006, 05:49 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21