Thread: variadic templates

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    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,895
    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, 03: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, 02:46 AM