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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:21:00+00:00 2026-06-18T00:21:00+00:00

As exercise, I am trying to write 2 simple functions taking a non-type template

  • 0

As exercise, I am trying to write 2 simple functions taking a non-type template int N.
The first one needs to create a tuple consisting of N copies of some object of type T. I would like it to resemble the following:

template <class T, int N> 
constexpr std::tuple<T...> fun(T t) {
  return (N > 0) ? std::tuple_cat(t, fun<T, N-1>(t)) : std::make_tuple(t);
}

I also tried something like this unsuccessfully (http://liveworkspace.org/code/3LZ0Fe).
I would like to be able to instantiate it with T = bool, say:

auto bools = fun<bool, 10> (false);

The second one should be a slight variation; I have a templated struct Foo and I would like to create a tuple containing a Foo< 0 >, …, Foo< N >

template <int N> struct Foo {
   static const int id = N;
}

template <template <int> class T, int N> 
constexpr ??? fun2 (???) {...}

Since templated functions can’t be partially specialized, I don’t even know how to write a proper termination for my recursion.
I would like to do this completely statically without using for loops.

==================================================================================

Following Seth’s suggestion I was stuck with writing the fun function itself which recursed infinitely:

template<typename T, int N>
typename fun_helper<T, N>::type 
fun(T t) {
  if (N > 0) 
    return typename fun_helper<T, N>::type 
      { std::tuple_cat(std::make_tuple(t), fun<T, N-1>(t)) };
  else return typename fun_helper<T, N>::type { };
}

By using an additional struct with a termination, I was able to get this working:

template<typename T, int N> struct fun {
  typename fun_helper<T, N>::type make(T t) {
    return typename fun_helper<T, N>::type 
      { std::tuple_cat(std::make_tuple(t), fun<T, N-1>().make(t)) };
  }
};

template<typename T> struct fun<T, 0> {
  typename fun_helper<T, 0>::type 
  make(T t) {
    return typename fun_helper<T, 0>::type { };
  }
};

The call is still relatively clumsy:

auto conds = fun<bool, 3>().make(false);

Is there a way to get it working without this additional struct ?

auto conds = fun<bool, 3>(false);
  • 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-18T00:21:01+00:00Added an answer on June 18, 2026 at 12:21 am

    For the first, you can recursively build a parameter pack using structs for partial specialisation (I show you this way because it relates to #2). This code isn’t tested and doesn’t take care of the default value for the elements, but it gives you the idea and the default value code can be easily added.

    template<typename T, int N, typename... Rest>
    struct fun_helper {
        typedef typename fun_helper<T, N - 1, T, Rest...>::type type;
    };
    
    template<typename T, typename... Rest>
    struct fun_helper<T, 0, Rest...> {
        typedef std::tuple<Rest...> type;
    };
    
    template<typename T, int N>
    typename fun_helper<T, N>::type fun() {
        return typename fun_helper<T, N>::type { };
    }
    

    For the second, you can combine the techniques above with a parameter pack of ints and use the ... to expand them like

    Foo<Ints>...
    

    which expands to

    Foo<Int1>, Foo<Int2>, ...
    

    in your function.

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

Sidebar

Related Questions

I'm trying to write a C++ application on Gnome/Ubuntu as my first non-academic exercise
As a small exercise I am trying to write a very small, simple game
I'm trying to write a simple baseball scorekeeping app, mostly as a learning exercise.
I'm trying (as an exercise) to create a simple numeric range class in C++.
I'm trying to do a simple exercise querying a database from JS using XMLHTTPrequest
I'm new to C and I'm trying to solve one of the exercise problem
I am trying to write a simple input field validation plugin at the moment
I am trying to write a Spider Solitaire player as an exercise in learning
I'm trying to write a simple HTTP remember me authentication system for users. My
Simple situation. I'm trying to write my own blog with a minor twist. Part

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.