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 8379845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:17:58+00:00 2026-06-09T16:17:58+00:00

Given the following code snippet from Anthony Williams . A very basic tuple example

  • 0

Given the following code snippet from Anthony Williams.
A very basic tuple example and everything in here works as expected.

#include <iostream>

template<typename ... Types>
class simple_tuple;

template<>
class simple_tuple<>
{};

template<typename First,typename ... Rest>
class simple_tuple<First,Rest...>:
        private simple_tuple<Rest...>
{
        First member;
public:
        simple_tuple(First const& f,Rest const& ... rest):
          simple_tuple<Rest...>(rest...),
                  member(f)
          {}
          First const& head() const
          {
                  return member;
          }
          simple_tuple<Rest...> const& rest() const
          {
                  return *this;
          }
};

template<unsigned index,typename ... Types>
struct simple_tuple_entry;

template<typename First,typename ... Types>
struct simple_tuple_entry<0,First,Types...>
{
        typedef First const& type;
        static type value(simple_tuple<First,Types...> const& tuple)
        {
                return tuple.head();
        }
};

template<unsigned index,typename First,typename ... Types>
struct simple_tuple_entry<index,First,Types...>
{
        typedef typename simple_tuple_entry<index-1,Types...>::type type;
        static type value(simple_tuple<First,Types...> const& tuple)
        {
                return simple_tuple_entry<index-1,Types...>::value(tuple.rest());
        }
};
template<unsigned index,typename ... Types>
typename simple_tuple_entry<index,Types...>::type
        get_tuple_entry(simple_tuple<Types...> const& tuple)
{
        std::cout << "SizeofArgs == " << sizeof...(Types) << std::endl;
        return simple_tuple_entry<index,Types...>::value(tuple);
}

int main()
{
        simple_tuple<int,char,double> st(42,'a',3.141);
        std::cout<<get_tuple_entry<0>(st)<<","
                <<get_tuple_entry<1>(st)<<","
                <<get_tuple_entry<2>(st)<<std::endl;
}

But I am wondering about the get_tuple_entry function.
I thought that the number of variadic template parameters would vary for each call, but the sizeof always returns 3.
So the function is somehow equivalent to the following(pseudo code)

template<unsigned index, <int,char,double> >
typename simple_tuple_entry<index, <int,char,double> >::type
        get_tuple_entry(simple_tuple<int,char,double> const& tuple)
{
        std::cout << "SizeofArgs == " << sizeof...(<int,char,double>) << std::endl;
        return simple_tuple_entry<index,<int,char,double> >::value(tuple);
}

But this would mean that get_tuple_entry is overloaded only by the return value which is not possible. Why is the signature different for each call?

  • 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-09T16:17:59+00:00Added an answer on June 9, 2026 at 4:17 pm

    But this would mean that get_tuple_entry is overloaded only by the return value which is not possible.

    get_tuple_entry is not a function, it’s a function template. What you refer to as three overloads of the same function that differ only in return type are not the same. They are distinct instantiations of the function template:

    get_tuple_entry<0, int, char, double>
    get_tuple_entry<1, int, char, double>
    get_tuple_entry<2, int, char, double>
    

    Which are not the same function.

    I thought that the number of variadic template parameters would vary for each call, but the sizeof always returns 3

    Of course. Each time you call an instantiation of that function template you pass the same argument of type simple_tuple<int,char,double>, so each time the template parameter pack is deduced as int, char, double which has size 3. The difference between the calls is that you call a different instantiation, and get_tuple_entry<0> is not the same as get_tuple_entry<1>, and each different instantiation returns a different element of the tuple.

    This is really no different to

    #include <iostream>
    
    template<int N>
    void func()
    {
        std::cout << N << '\n';
    }
    
    int main()
    {
        func<0>();
        func<1>();
        func<2>();
    }
    

    This calls three different functions that print different things, but there’s no problem with identical signatures, because func<0>() and func<1>() and func<2>() are all different functions. If you look at the mangled names you’ll see they have different signatures, e.g. with G++ I get _Z4funcILi0EEvv and _Z4funcILi1EEvv and _Z4funcILi2EEvv which are not the same signature.

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

Sidebar

Related Questions

Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
Given the following code snippet: $i= 11; function get_num() { global $i; return (--$i
The following code snippet works fine in 1.8.7 on Mac OS X, but not
following snippet is from rails code def rescue_from(*klasses, &block) options = klasses.extract_options! unless options.has_key?(:with)
I have the following code snippet from my PowerShell script that... Loops through a
Question: Given the following code snippet: bool foo(int n) { for(int i=3;i<sqrt(n)+0.5;i+=2) { if((n%i)==0){
Given the following code snippet: using System; using Foo = System.Int32; namespace ConsoleApplication3 {
The following snippet is code from water-nsq benchmark from SPLASH 2 ... if (comp_last
Given the following code snippet: try { var myTxt = ; var serializer =
The following is a Ruby code snippet from Why's Poignant Guide to Ruby Chapter

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.