variadic templates

This is a discussion on variadic templates within the C++ Programming forums, part of the General Programming Boards category; I've just started playing with some of the C++0x extensions offered by G++ 4.3 on ubuntu 9.04, and it seems ...

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

    variadic templates

    I've just started playing with some of the C++0x extensions offered by G++ 4.3 on ubuntu 9.04, and it seems there is no way of iterating over the list of parameters in a variadic template function. I can get the number of template parameters with the sizeof...() operator, but there seems to be no way to get the value of a specific parameter.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You write a recursive template for iteration. The basic pattern looks like this:
    Code:
    template <typename... Args> struct meta_func;
    template <> struct meta_func<> {}; // Terminator case
    template <typename Head, typename... Tail>
    struct meta_func<Head, Tail> {
      // Deal with Head
    };
    You can probably use Boost.MPL for more convenient access, actually.
    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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I haven't played with them yet, but surely there is a way to do it. Have you searched the documentation?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Manipulating Args Passed to Variadic Functions
    By CandyMan80211 in forum C Programming
    Replies: 1
    Last Post: 07-05-2007, 04:35 PM
  3. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  4. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 03:46 AM

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