开发者

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

开发者 https://www.devze.com 2023-04-06 11:28 出处:网络
To illustrate: struct MyFunc { template <size_t N> void doIt() { cout << N << endl; } }; template <typename Func>

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)


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

f.template doIt<123>();
0

精彩评论

暂无评论...
验证码 换一张
取 消