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

  • SEARCH
  • Home
  • 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 6591267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:24:10+00:00 2026-05-25T17:24:10+00:00

A hypothetical variadic template tuple class would, as far as I can tell, have

  • 0

A hypothetical variadic template tuple class would, as far as I can tell, have to use getters with template parameters.

int MyInt = MyTuple.Get<int>(0);

This is inconvenient, and introduces potential for error. I can’t help but feel that there’s a way to construct the class so you you don’t have to explicitly specify it.

int MyInt = MyTuple.Get(0);

My first thought was for the Get() member function to return another class that figures out the type on its own, probably by comparing typeid(Foo).name() to values in some precomputed list. That still has to happen before runtime, though, and I couldn’t figure out a way to iterate through anything like that at compile-time.

Is there any way for a variadic template container class – like a tuple – to have a getter that doesn’t require the type to be explicitly specified?

  • 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-05-25T17:24:11+00:00Added an answer on May 25, 2026 at 5:24 pm

    You mean like std::tuple?

    The getter’s template argument specifies the index of the member, not the type. By definition, the number and types of a tuple are fixed at compile time. Since the type depends on the index, and the type must be known at compile time, the getter must be templated.

    template< typename ... types >
    struct tuple;
    
    template< typename head, typename ... tail_types >
    struct tuple {
        head value;
        tuple< tail_types ... > tail;
    };
    
    template<>
    struct tuple<> {};
    
    template< typename tuple, size_t index >
    struct tuple_element;
    
    template< typename head, typename ... tail, size_t index >
    struct tuple_element< tuple< head, tail ... >, index >
        { typedef typename tuple_element< tail ..., index - 1 >::type type; };
    
    template< typename head, typename ... tail >
    struct tuple_element< tuple< head, tail ... >, 0 >
        { typedef head type; };
    
    template< size_t index, typename tuple >
    typename std::enable_if< index != 0, 
                       typename tuple_element< tuple, index >::type >::type
    get( tuple const &t )
        { return get< index - 1 >( t.tail ); }
    
    template< size_t index, typename tuple >
    typename std::enable_if< index == 0,
                       typename tuple_element< tuple, index >::type >::type
    get( tuple const &t )
        { return t.value; }
    

    etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following hypothetical code: class User < ActiveRecord::Base def oauth_consumer @oauth_consumer ||=
This is a rather hypothetical question, but let's say I have 3 long parameters
Hypothetical situation: let's say I have a 3rd party .net assembly being used in
Hypothetical situation. I have a command line program in *nix (linux, BSD, etc.). It
Consider a hypothetical method of an object that does stuff for you: public class
Take the following hypothetical situation, how would I implement it in MVC? All my
While fleshing out a hypothetical domain model, I have found myself wondering whether the
Given an hypothetical utility class that is used only in program setup: class MyUtils
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
This hypothetical example illustrates several problems I can't seem to get past, even though

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.