Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass template arguments to a function on an object when the object type is a template argument?

To illustrate:

struct MyFunc {

    template <size_t N>
    void doIt() {
        cout << N << endl;
    }

};

template <typename Func>
struct Pass123ToTemplateFunc {

    static void pass(Func f) {
        f.doIt<123>(); // <-- Error on compile; is there a way to express this?
    }

};

int main() {

    Pass123ToTemplateFunc<MyFunc>::pass(MyFunc());

    return 0;

}

This is pretty much purely a syntax curiosity; is there a way in the language to express this without passing arguments to the doIt function itself? If not, it's no big deal and I'm already well aware of ways I can gracefully work around it, so no need to provide alternative solutions. (I'll accept "no" as an answer, in other words, if that's the truth. :-P)

like image 367
nonoitall Avatar asked Sep 22 '11 09:09

nonoitall


1 Answers

You have to tell the compiler that doIt will be a template:

f.template doIt<123>();
like image 54
sth Avatar answered Sep 22 '22 16:09

sth