Is there a generic way to adapt a function template to be a polymorphic function object?

后端 未结 2 823
清歌不尽
清歌不尽 2020-12-14 21:42

I have some function templates, for example

template 
void foo(T);

template 
void bar(T);

// others

a

2条回答
  •  渐次进展
    2020-12-14 22:48

    Why couldn't you use template template parameters? You said you can't pass your template uninstantiated, but I'm not sure if you've heard of this before, tell me if you have and it won't work.

    I don't know what your code structure looks like, but can you do something like

    I know this works, don't know if it's what yo uwant though:

    template
    T some_algorithm(T data) { return T(); } // just returning nothing for example
    
    template
    class FuncClass {
    public:
        T run(T data) { return Something(data); }
    };
    
    template
    void apply_algorithm(T data) {
        Functor F;
        F.run(data);
    }
    
    int main() {
        int mydata = 4;
        apply_algorithm > >(mydata);
    
        cin.get();
    }
    

提交回复
热议问题