Template function as a template argument

后端 未结 4 427
庸人自扰
庸人自扰 2020-12-01 02:03

I\'ve just got confused how to implement something in a generic way in C++. It\'s a bit convoluted, so let me explain step by step.


Consider such code:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 02:47

    In order to solve this problem with templates, you have to use a template template parameter. Unfortunately, you cannot pass template template function as a type, because it has to be instantiated first. But there is a workaround with dummy structures. Here is an example:

    template 
    struct a {
    
        static void foo (T = T ())
        {
        }
    
    };
    
    template 
    struct b {
    
        static void foo (T = T ())
        {
        }
    
    };
    
    struct SomeObj {};
    struct SomeOtherObj {};
    
    template