Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:27+00:00 2026-06-13T12:59:27+00:00

In a recent mail on the boost developers mailing list Eric Niebler mentioned that

  • 0

In a recent mail on the boost developers mailing list Eric Niebler mentioned that it is possible to get the last element of a template parameter pack in O(1) instantiations.

How can this be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:59:28+00:00Added an answer on June 13, 2026 at 12:59 pm

    You can check https://github.com/ericniebler/proto-0x/blob/master/boost/proto/utility.hpp, as mentioned in the post, on how the get_nth metafunctions are implemented. The essence, after much simplification:

    #include <type_traits>
    #include <cstdlib>
    
    // A template to store a variadic list.
    template <typename...>
    struct List;
    
    // Concatenate two variadic lists together. Should be O(1).
    template <typename Left, typename Right>
    struct Concat;
    
    template <typename... Lefts, typename... Rights>
    struct Concat<List<Lefts...>, List<Rights...>> {
        typedef List<Lefts..., Rights...> Type;
    };
    
    // Construct List<void, void, void, ...> with 'n' elements.
    // Uses "bisection" method, which should instantiate O(log n) templates
    template <size_t n>
    struct RepeatVoid : Concat<typename RepeatVoid<n/2>::Type,
                               typename RepeatVoid<n - n/2>::Type> {};
    
    template <>
    struct RepeatVoid<0> {
        typedef List<> Type;
    };
    
    template <>
    struct RepeatVoid<1> {
        typedef List<void> Type;
    };
    

    template <typename>
    struct GetNthAux;
    
    // This would create a function of the form:
    //
    //   T eval(cv void*, cv void*, cv void*, ..., cv void*, T*, ...)
    //
    // where there are 'n' const volatile void*. These will be used to absorb
    // the first 'n' template arguments. The actual one can be extracted from
    // the return type.
    //
    template <typename... Ts>
    struct GetNthAux<List<Ts...>> {
        template<typename T>
        static constexpr T eval(const volatile Ts*..., T*, ...);
    };
    
    // Run the whole thing.
    template<size_t n, typename... Ts>
    using GetNth = decltype(
        GetNthAux<typename RepeatVoid<n>::Type>::eval((Ts*)nullptr...)
    );
    
    // Example:
    int main() {
        static_assert(std::is_same<GetNth<0, int, float, char>, int>::value, "");
        static_assert(std::is_same<GetNth<1, int, float, char>, float>::value, "");
        static_assert(std::is_same<GetNth<2, int, float, char>, char>::value, "");
    }
    

    Actually it is O(log N), not O(1), because of the time taken in RepeatVoid.

    But this method does perform much better than linear methods like the typical implementation of tuple_element<>. I have tried to compare the performance of GetNth with tuple_element<> on getting the item 899 of a list, and the latter is an order of magnitude faster:

                             tuple_element<>   GetNth<>
    g++ 4.7 with libstdc++   0.7s              0.1s
    clang++ 3.1 with libc++  0.9s              0.1s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A recent post by John Gruber notes that the following legalese: 3.3.1 — Applications
A recent question contains a problem that I many times used to think about
In a couple of recent projects, I've written an e-mail queue as a database
I'm using zend mail extended with zend_mail_storage_imap and I built an application that looks
Years back I built a simple mail form that has been working like a
Is it possible to send mail asycronously using PHP while giving live user feedback
Recent iPads are having provision to insert sim card in that. So with latest
I have received an mail from Apple that my app has been rejected. The
I've been trying to get my application to mail some outputted text to an
I have a script that goes into Fedex Billing each week when they mail

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.