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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:48:46+00:00 2026-05-18T09:48:46+00:00

I would like to have a type, that would be in effect POD but

  • 0

I would like to have a type, that would be in effect POD but I would like to be able to specify how and which types are in it, for example:

template<Args...>
struct POD
{
//here I would like to create depending on Args appropriate types as a members.
};

Is it possible to do it with this new variadic templates feature in C++0x?

  • 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-18T09:48:46+00:00Added an answer on May 18, 2026 at 9:48 am

    I’ve never yet used the C++0x variadic templates feature but the following code compiles on G++ 4.5:

    template <typename... Args>
    struct tuple;
    
    template <typename T, typename... Args>
    struct tuple<T, Args...> {
        T value;
        tuple<Args...> inner;
    };
    
    template <typename T>
    struct tuple<T> {
        T value;
    };
    

    However, initializing them is … weird because we need to nest the inner values:

    int main() {
        tuple<int> n1 = { 23 };
        tuple<int, float> n2 = { 42, { 0.5f } };
        tuple<std::string, double, int> n3 = { "hello, world", { 3.14, { 97 } } };
    }
    

    Retrieving the values is of course a bit tedious. The simplest method is probably to provide a get<N>() function template.

    But we cannot implement get directly since function templates cannot be partially specialized. Either we need to use SFINAE (read: boost::enable_if) or we need to delegate the actual function of get to a type that can be partially specialized.

    In the following, I did the latter. But first, we need another helper type trait: nth_type, which returns the appropriate return type of the get function:

    template <unsigned N, typename... Args>
    struct nth_type;
    
    template <unsigned N, typename T, typename... Args>
    struct nth_type<N, T, Args...> : nth_type<N - 1, Args...> { };
    
    template <typename T, typename... Args>
    struct nth_type<0, T, Args...> {
        typedef T type;
    };
    

    Easy-peasy. Just returns the nth type in a list of types.

    Now we can write our get function:

    template <unsigned N, typename... Args>
    inline typename nth_type<N, Args...>::type get(tuple<Args...>& tup) {
        return get_t<N, Args...>::value(tup);
    }
    

    Like I said, this just delegates the task. No biggie. In practice, we probably want to have another overload for const tuples (but then, in practice we would use an existing tuple type).

    Now for the killing, followed by a light salad:

    template <unsigned N, typename... Args>
    struct get_t;
    
    template <unsigned N, typename T, typename... Args>
    struct get_t<N, T, Args...> {
        static typename nth_type<N, T, Args...>::type value(tuple<T, Args...>& tup) {
            return get_t<N - 1, Args...>::value(tup.inner);
        }
    };
    
    template <typename T, typename... Args>
    struct get_t<0, T, Args...> {
        static T value(tuple<T, Args...>& tup) {
            return tup.value;
        }
    };
    

    And that’s it. We can test this by printing some values in our previously defined variables:

    std::cout << get<0>(n1) << std::endl; // 23
    std::cout << get<0>(n2) << std::endl; // 42
    std::cout << get<0>(n3) << std::endl; // hello, world
    
    std::cout << get<1>(n2) << std::endl; // 0.5
    std::cout << get<1>(n3) << std::endl; // 3.14
    
    std::cout << get<2>(n3) << std::endl; // 97
    

    Man, it’s fun messing with variadic templates.

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

Sidebar

Related Questions

I would like to have a TextBox that supports AutoComplete and lets users type
I would like to have a name link type effect where instead of it
I would like to simulate the effect of the following type of query (that
I have a Show table, and I would like to have a derived type
I am working in Filemaker 12, I would like to have a special type
I have a variable of type sbyte and would like to copy the content
I have a search form. I would like when someone type the term, first
So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:
I would like to have a field which is updated on every change(insertion, modification),

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.