在模板类中实例化一个自由模板函数

时间:2011-07-07 11:13:28

标签: c++ delegates rpc library-design

我需要在模板类(TC)中实例化一个自由模板函数(FTF)。 FTF将TC的模板参数之一作为模板参数。 TC还保存了这些FTF的通用指针,这些函数通过指针调用。

获取指向FTF的指针的步骤不足以实例化它,并且我从GCC工具链收到链接器错误。 MSDN说明了FTF规范as so - 但是我对FTF的实例依赖于我的TC的模板参数,因此FTF实例化不能放在自由范围内。

这可能吗?我附加了一些基本的生成代码,问题出在class test_service的构造函数中,我将自由函数的指针分配到自定义容器中。我收到链接器错误,告诉我无法找到免费功能(未实例化)。我知道在某个类中指定对模板函数的调用会产生实例化,但是我只会通过指针进行调用。

#include "rpc_common.h"
#include <boost/cstdint.hpp>

namespace rubble { namespace rpc {

  struct test_service_dummy_tag{};

  template<typename T>
  class test_service_skel
  {
  public:
    bool Init() {}
    bool TearDown() {}
    bool test_one(TestRequest,TestResponse){};
  private:
  };

  template<typename T_IMPL>
  bool test_service_test_one(T_IMPL & impl,ClientRequest & request)
  {
    return 0;
  }

  template<typename T_IMPL=test_service_skel<test_service_dummy_tag> >
  class test_service
  {
  public:
    test_service()
    {
      // uncomment the following two lines and a instantiation will occur.
      // ClientRequest cr;
      //test_service_test_one<T_IMPL>(m_impl,cr);
      m_dispatch_table.SetEntry( Oid("test_one",0),(void *)  & test_service_test_one<T_IMPL>);
    }
    bool Init() { return m_impl.Init(); };
    bool TearDown() { return m_impl.TearDown(); };
  private:
    T_IMPL m_impl;
    OidContainer<Oid,void *> m_dispatch_table;
  };


} }

编辑:自包含的最小版本

 class test_skel
    {
      bool test_function()
      {
        return true;
      }
    };


    template<typename T>
    bool test_function()
    {

    }

    template<typename T = test_skel>
    class test
    {
    public:
      test()
      {
        dispatch = (void *) & test_function<T>;
      }
      void * dispatch;
    };

    int main()
    {
      test<> t;
      return 0;
    }

2 个答案:

答案 0 :(得分:2)

如果您不使用void*,即:http://www.ideone.com/eRgUG

,则没有问题

但是,如果坚持将指针存储在void*中,那么您需要先使用特定的函数指针获取地址,然后再进行转换 - 例如

    bool (*temp)() = &test_function<T>;
    dispatch = reinterpret_cast<void*>(temp); // YUCK

这为编译器提供了足够的上下文来为您生成地址。

啊 - 刚看到DeadMG的答案,生成void*的功能更整洁......

答案 1 :(得分:1)

对于我来说,当没有重载函数的奇怪错误时,你的自包含示例将无法编译,当没有超载时,使用MSVC。但是,我确实设法解决了这个问题。

class test_skel
{
    bool test_function()
    {
        return true;
    }
};

template<typename T> void* to_void_pointer(T t) {
    return reinterpret_cast<void*>(t);
}

template<typename T>
bool test_function()
{
    return true;
}

template<typename T = test_skel>
class test
{
public:
    test()
    {
        dispatch = to_void_pointer(&test_function<T>);
    }
    void * dispatch;
};

int main()
{
    test<> t;
    return 0;
}

这个编译得很干净。我怀疑你所看到的任何行为都是编译错误。

相关问题