Templated operator () C ++ overload

someone already asked this question, but the thread ended up in the original question, but received no answer.

suppose you have this:

template<size_t i, class f_type>
void call_with_i(f_type f);

functor_type:

a) struct with a method that has the following signature:

template<size_t i> operator()() const;

 or, b) a function that looks like this:

template<size_t i> foo();

Strike>

I want "call_with_i <42> (foo)" to be equivalent to "foo <42> ()", but I cannot figure out the correct syntax for this to happen. I would be satisfied with a solution that only makes (a) , but (a) + (b) would be great . I have already tried these syntaxes:

f< i >(); // doesn't work
f()< i >; // doesn't work
f.operator< i >(); // doesn't work
f.operator()< i >; // doesn't work
f.operator()< i >(); // works on msvc, but doesn't work on gcc. 

How do you call operator () with explicit template arguments? Is there a way to call it so that the same syntax also calls a template free function?

p.s. , , , repeat_to, repeat_to < 10 > (f) f (0), f (1)... f (10). boost:: fusion . , , -, .

: , .

+8
3

, f_type. , "" ( "" ), , typename :

template<size_t i, class f_type>
void call_with_i(f_type f) {
  f.template operator()<i>();
  // f.template foo<i>();
}

:

template<size_t N> struct size_t_ { }; // or boost::mpl::int_

template<size_t i, class f_type>
void call_with_i(f_type f) {
  f(size_t_<i>());
}

operator() :

template<size_t i> void operator()(size_t_<i>) const {
  // i was deduced automatically by the function argument. 
}

, f_type()<i>() - . .

+13

, , boost:: function . , , .

+1
#include <iostream>

template<size_t i, class f_type> void call_with_i(f_type f);

struct A {

    template < size_t i >
    void operator()() const {
        /* no link err in demo */
    }

    template < size_t i >
    void foo() {
        /* no link err in demo */
    }
};

int main(int argc, char * const argv[]) {
    A f;

    enum { Constant = 42 };

    f.operator()<Constant>();
    f.foo<Constant>();

    return 0;
}

Is there a way to call it in such a way that the same syntax also calls a template free function?

Can you clarify? (pseudo code or something else)

0
source

Source: https://habr.com/ru/post/1626063/


All Articles