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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:35:30+00:00 2026-06-06T08:35:30+00:00

The following simple code pattern is very common in graphics programming. It creates an

  • 0

The following simple code pattern is very common in graphics programming.
It creates an array of layers and loops over them.

struct Layer
{
    int n;
    void operator()(float value)
    {
    }
};

struct AnotherLayer
{
    int n;
    int m;
    void operator()(float value)
    {
    }
};

void process_layers(Layer_t* layer, size_t size, float value) 
{
    for (size_t n = 0; n < size; ++n)
        layer[n](value);
}

Layer a = {1};
Layer b = {2};
AnotherLayer c = {2,3};
typedef std::function < void (float) > Layer_t;
Layer_t layers [] = {a,b,c};
process_layers(layers, sizeof(layers)/sizeof(Layer), 100);

I would like to convert this to use varadic templates in c++11. Any ideas how I could do this. This is what I would like it to look like. Any ideas? Is this even possible?

template <int n>
struct Layer
{
    void operator()(float value)
    {
    }
};

template <int n, int m>
struct AnotherLayer
{
    void operator()(float value)
    {
    }
};

template <typename Layer1, typename Layer2, ...>
struct Layers  //process_layers
{
    void operator()(float value)
    {
        for (size_t n = 0; n < SIZEOF(Layer1,Layer2,...); ++n)
            Layer[N]()(value);
    }
};

Then I could do this.

typedef Layers<Layer<1>, Layer<2>, AnotherLayer<3,8> > funky_layer_t;
typedef Layers<Layer<4>, Layer<5>, Layer<5>, AnotherLayer<6,7> > standard_layer_t;
typedef Layers<funky_layer_t, standard_layer_t> awesome_layer_t;

awesome_layer_t()(100);

Note: with the second approach, all paramaters to construct layers are known at compile time.

  • 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-06T08:35:31+00:00Added an answer on June 6, 2026 at 8:35 am

    The example you give is very simple to re-do with variadic templates.

    template<typename Func> void process(Func &&f) {} // base case for zero items
    
    // overload for at least one item
    template<typename Func, typename FirstItem, typename... Items>
    void process(Func &&f, FirstItem &&fi, Items &&...is) {
        std::forward<Func>(f)(std::forward<FirstItem>(fi));          // f(fi);
        process(std::forward<Func>(f), std::forward<Items>(is)...);  // process(f,is...);
    }
    
    Layer a = {1};
    Layer b = {2};
    Layer c = {3};
    
    process([](Layer &l){ l(100); },
            a, b, c);
    

    Also notice that this avoids all the unnecessary copies in the original. (Though of course you could also avoid them just by doing Layer layers[] = {{1},{2},{3}};)


    I’m not exactly sure how your later comments and code are related to running an operation over a collection of layers.

    What exactly is the computation you want to perform at compile-time?


    To adjust for the new example the process() does not need to change at all, you only need to create a functor that can handle each type. (polymorphic lambdas would help here, but we’ll have to make due with an explicit functor type)

    Layer a = {1};
    Layer b = {2};
    AnotherLayer c = {2,3};
    
    struct TheOperation {
        template<typename T>
        void operator() (T &t) {
            t(100);
        }
    };
    
    process(TheOperation(),
        a, b, c);
    

    Here’s your awesome_layer_t transcribed to correct variadic template syntax, but I still don’t see what you want to accomplish, so I can’t say if this is a good way to do it or not. This doesn’t actually call the operator()s at compile-time, it only arranges to have a bunch of objects default constructed at runtime and then operator() called, again, at runtime.

    template <int n>
    struct Layer
    {
        int operator()(float value)
        {
            std::printf("L %d %e\n",n,value);
            return 0;
        }
    };
    
    template <int n, int m>
    struct AnotherLayer
    {
        int operator()(float value)
        {
            std::printf("AL %d %d %e\n",n,m,value);
            return 0;
        }
    };
    
    template <typename... Ls>
    struct Layers  //process_layers
    {
        int operator()(float value)
        {
            struct Tmp {
                void operator() (...) {}
            };
            Tmp()( Ls()(value)...);
            return 0;
        }
    };
    
    typedef Layers<Layer<1>, Layer<2>, AnotherLayer<3,8> > funky_layer_t;
    typedef Layers<Layer<4>, Layer<5>, Layer<5>, AnotherLayer<6,7> > standard_layer_t;
    typedef Layers<funky_layer_t, standard_layer_t> awesome_layer_t;
    
    int main() {
        awesome_layer_t()(100);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following very simple code snippet which is loaded from a separate
I'm using the following simple Go code to allocate a 3D array of size
Consider the following simple code pattern: foreach(Item item in itemList) { if(item.Foo) { DoStuff(item);
I have a following simple code: def get(): return [lambda: i for i in
I've got the following simple code: @for (int j = 0; j < file.Items.Count;
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I have the following simple code abstract class A { public abstract void Test(Int32
I am trying to run the following simple code, public abstract class Shape{ abstract
Just wondering, having the following simple code: var object1 = { name: function (){
I have the following simple Java code: package testj; import java.util.*; public class Query<T>

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.