Template function as a template argument

后端 未结 4 399
庸人自扰
庸人自扰 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条回答
  • 2020-12-01 02:46

    Here's a way. It may not be the best, but it works:

    template <typename T, T param>
    void function() {
        param(123);
        param(456);
    }
    
    void test()
    {
        function< void(*)(int), a<int> >(); // space at end necessary to compiler
        function< void(*)(int), b<int> >(); // because the C++ grammar is ambiguous
    }
    

    Whether or not they'll be inlined depends on the compiler, but I would be rather surprised if they weren't.

    EDIT: Okay, I'm a little off today and missed the part where the parameters are of different types. My bad.

    There may be a tricky way to do this with templates, but this is the easiest way I could think of:

    #define function(x) do { x<thing1>(obj1); x<thing2>(obj2) } while(0)
    

    I know, I know, "macros are evil," blah blah blah. It works. If function needs to be more complicated than your example you may run into problems, but it is much easier than anything I've been able to come up with.

    0 讨论(0)
  • 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 <typename T>
    struct a {
    
        static void foo (T = T ())
        {
        }
    
    };
    
    template <typename T>
    struct b {
    
        static void foo (T = T ())
        {
        }
    
    };
    
    struct SomeObj {};
    struct SomeOtherObj {};
    
    template <template <typename P> class T>
    void function ()
    {
        T<SomeObj>::foo ();
        T<SomeOtherObj>::foo ();
    }
    
    int main ()
    {
        function<a>();
        function<b>();
    }
    
    0 讨论(0)
  • 2020-12-01 02:50

    With generic lambda from C++14 you might do:

    template<typename T> void a(T t) { /* do something */}
    template<typename T> void b(T t) { /* something else */ }
    
    template <typename F>
    void function(F&& f) {
        f(someobj);
        f(someotherobj);
    }
    
    void test() {
        // For simple cases, auto&& is even probably auto or const auto&
        function([](auto&& t){ a(t); });
        function([](auto&& t){ b(t); });
    
        // For perfect forwarding
        function([](auto&& t){ a(std::forward<decltype(t)>(t)); });
        function([](auto&& t){ b(std::forward<decltype(t)>(t)); });
    }
    

    Can compilers still inline the calls if they are made via function pointers?

    They can, but it is indeed more complicated, and they may fail more often than with functor or template.

    0 讨论(0)
  • 2020-12-01 02:52
    template < typename F >
    void function(F f)
    {
      f(123);
    }
    
    void a(int x) { ... }
    
    struct b { void operator() (int x) { ... } };
    
    void outer()
    {
      function(&a);
      function(b());
    }
    
    0 讨论(0)
提交回复
热议问题