簡體   English   中英

成員函數模板參數的部分專業化?

[英]Partial specialization for member functions template arguments?

成員函數模板參數的部分專業化可能嗎? 我不確定,但是值得一試。 這很好用( http://coliru.stacked-crooked.com/a/795e953af7537ab6 ):

#include <iostream>

struct Foo { void Bar() { std::cout << "Bar()" << std::endl; } };
struct Foo2 { void Bar2() { std::cout << "Bar2()" << std::endl; } };

template <typename TClass, void(TClass::*t_func)()>
void function()
{
    TClass p;
    (p.*t_func)();
}

template <typename TClass, void(TClass::*t_func)(), typename TClass2, void(TClass2::*t_func2)(), typename ...Rest>
void function()
{
    function<TClass, t_func>();
    function<TClass2, t_func2, Rest...>();
}

int main()
{
    function<Foo, &Foo::Bar, Foo2, &Foo2::Bar2>();
}

輸出:

Bar()
Bar2()

下一個示例也可以使用( http://coliru.stacked-crooked.com/a/e8fe14dde6932f4c ):

#include <iostream>

struct Foo { Foo() { std::cout << "Foo()" << std::endl; } };
struct Foo2 { Foo2() { std::cout << "Foo2()" << std::endl; } };

template <typename... Args>
struct Impl;

template <typename TClass, typename ...Rest>
struct Impl<TClass, Rest...>
{
    static void function()
    {
        TClass p;
        Impl<Rest...>::function();
    }
};

template <>
struct Impl<>
{
    static void function(){ std::cout << "EmptyImpl" << std::endl; }
};

template <typename ...Args>
struct Factory
{
    Factory()
    {
        Impl<Args...>::function();
    }
};

int main()
{
    auto f = Factory<Foo, Foo2>();
}

輸出:

Foo()
Foo2()
EmptyImpl

但是,如果我想結合以上兩件事:

#include <iostream>

struct Foo { void Bar() { std::cout << "Bar()" << std::endl; } };
struct Foo2 { void Bar2() { std::cout << "Bar2()" << std::endl; } };

template <typename... Args>
struct Impl;

template <typename TClass, void(TClass::*t_func)(), typename ...Rest>
struct Impl<TClass, decltype(t_func), Rest...>
{
    static void function()
    {
        TClass p;
        Impl<Rest...>::function();
    }
};

template <>
struct Impl<>
{
    static void function(){}
};

template <typename ...Args>
struct Factory
{
    Factory()
    {
        Impl<Args...>::function();
    }
};

int main()
{
    auto f = Factory<Foo, &Foo::Bar, Foo2, &Foo2::Bar2>();
}

這將是一個編譯器錯誤:

main.cpp: In function 'int main()':
main.cpp:36:59: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... Args> struct Factory'
         auto f = Factory<Foo, &Foo::Bar, Foo2, &Foo2::Bar2>();
main.cpp:36:59: note:   expected a type, got '&Foo::Bar'
main.cpp:36:59: error: type/value mismatch at argument 1 in template parameter list for 'template<class ... Args> struct Factory'
main.cpp:36:59: note:   expected a type, got '&Foo2::Bar2'

任何想法? 我錯過了什么? :)

考慮使用其他包裝器來調用成員函數

#include <iostream>

struct Foo { void Bar() { std::cout << "Bar()" << std::endl; } };
struct Foo2 { void Bar2() { std::cout << "Bar2()" << std::endl; } };

template <class TClass>
using FType = void(TClass::*)();

template<class TClass, FType<TClass> t_func>
class Caller
{
public:
  Caller()
  {
    TClass p;
    (p.*t_func)();
  }
};

template <typename... Args>
struct Impl;

template <class TClass, typename ...Rest>
struct Impl<TClass, Rest...>
{
    static void function()
    {
        TClass p;
        Impl<Rest...>::function();
    }
};


template <>
struct Impl<>
{
    static void function(){}
};

template <class ...Args>
struct Factory
{
    Factory()
    {
        Impl<Args...>::function();
    }
};

int main()
{
    auto f = Factory<Caller<Foo, &Foo::Bar>, Caller<Foo2, &Foo2::Bar2> >();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM