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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:07:27+00:00 2026-06-13T16:07:27+00:00

I am trying to run a compile-time iterator like: meta::reverse_iterator<2, 9>::iterate(callback()); meta::reverse_iterator<4, 7>::iterate(callback()); std::cout

  • 0

I am trying to run a compile-time iterator like:

meta::reverse_iterator<2, 9>::iterate(callback());
meta::reverse_iterator<4, 7>::iterate(callback());
std::cout << "-----------------" << std::endl;
meta::iterator<2, 9>::iterate(callback());
meta::iterator<4, 7>::iterate(callback());

struct callback {
  template <int i>
  void operator()() {
    std::cout << "print !!" << i << std::endl;
  }
};

and this is how I’ve written the meta-iterator:

namespace meta {
    template <int Begin, int End, bool done = false>
    struct reverse_iterator {
        template <typename F>
        static void iterate(F f) {
            f.template operator()<End>();
            reverse_iterator<Begin, End-1, Begin == End-1>::iterate(f);
        }
    };

    template <int Begin, int End>
    struct reverse_iterator<Begin, End, true> {
        template <typename F>
        static void iterate(F) {}
    };

    template <int Begin, int End, bool done = false>
    struct iterator {
        template <typename F>
        static void iterate(F f) {
            iterator<Begin, End - 1, Begin == End - 1>::iterate(f);
            f.template operator()<End - 1>();
        }
    };

    template <int Begin, int End>
    struct iterator<Begin, End, true> {
        template <typename F>
        static void iterate(F) {}
    };

}

Right now I iterator calls operator()<N> But I want it to be able to call any arbitrary function supplied by user with template parameter <N> (not as run-time argument) How can that be achieved ?

also boost::bind doesn’t work with it as it calls the function object of bind instead of the real function. So there should be some way to carry default parameters to the supplied functions.

  • 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-13T16:07:28+00:00Added an answer on June 13, 2026 at 4:07 pm

    AFAIK you can’t do it directly, but if you really want it you can use a traits class that call member function for you, and for different member functions write different traits:

    struct function_operator_call_trait {
        template< int N, class T >
        void call( T& t ) {t.template operator()<N>();}
    };
    template< class Arg1, class Arg2 >
    struct foo_call_trait {
        foo_call_trait( Arg1&& arg1 ) : a1( std::move(arg1) ) {}
        foo_call_trait( Arg1&& arg1, Arg2&& arg2 )
            : a1( std::move(arg1) ), a2( std::move(arg2) ) {}
        template< class T, int N >
        void call( T& t ) {t.template foo<N>(a1, a2);}
        Arg1 a1;
        Arg2 a2;
    };
    template <int Begin, int End, class traits = function_operator_call_trait, bool done = false>
    struct iterator{
        iterator() {}
        iterator( traits const& t ) : t_( t ) {}
        iterator( traits&& t ) : t_( std::move(t) ) {}
        template<typename F>
        static void iterate(F f){
            iterator<Begin, End-1, traits, Begin == End-1>::iterate(f);
            t_.call<End-1>( f );
        }
        traits t_;
    };
    typedef iterator<2, 9> fc_iterator;
    typedef foo_call_trait<int, float> foo_traits;
    typedef iterator<2, 9, foo_traits> foo_iterator( foo_traits(1, 2) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how to select the right overloaded function template at compile-time
Having this errors when trying to compile/run my app using Maven. I've succeeded to
I'm trying to run NDK to compile native code to run on Atom(x86) processor
I'm trying to compile and run XROTOR on my Windows 7 laptop. I have
I am trying to compile and run a simple java class within eclipse. The
I am trying to compile a project to run on an ARM board that
I'm trying to get a compile command (rake cucumber) to run with a specific
I'm trying to compile cld , when I run ./configure && make I got
I'm trying to compile fortran code given to me by a labmate to run
I am trying to compile Webkit on Ubuntu 8.04. But when i run autogen.sh,

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.