Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to pass a function which has variable arguments as template argument?

how to pass a function which has variable arguments as template argument?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorialquestionlounge
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falconapollo
    wrote on last edited by
    #1

    I want to write a adapter which can convert non static member functions to C-sytle function pointers. Here is what I got now(see the following code snippet), but the current solution is not general. I want to make

    int (T::*Func)(int)

    accept variable arguments. but i'm stuck here? anybody can help me? and it's necessary to make

    int f()

    and

    int display

    have the same signature. The final goal is Interfacing C++ member functions with C libraries.

    class StoreVals
    {
    int val;
    public:
    int display(int k) { cout << k << endl; return 0; }
    };

    template
    class CObjectT
    {
    public:
    /*
    the signagure of f(...) should change by the argument of template. they must be the same, but i don't know how to achieve this goal
    */
    static int f(int i)
    {
    T obj;
    return (obj.*Func)(i);
    }
    };

    void main()
    {
    CObjectT::f(7);
    auto function_t = &CObjectT::f;
    cout << typeid(function_t).name() << endl; // now it's a c-style function pointer
    }

    J 1 Reply Last reply
    0
    • F Falconapollo

      I want to write a adapter which can convert non static member functions to C-sytle function pointers. Here is what I got now(see the following code snippet), but the current solution is not general. I want to make

      int (T::*Func)(int)

      accept variable arguments. but i'm stuck here? anybody can help me? and it's necessary to make

      int f()

      and

      int display

      have the same signature. The final goal is Interfacing C++ member functions with C libraries.

      class StoreVals
      {
      int val;
      public:
      int display(int k) { cout << k << endl; return 0; }
      };

      template
      class CObjectT
      {
      public:
      /*
      the signagure of f(...) should change by the argument of template. they must be the same, but i don't know how to achieve this goal
      */
      static int f(int i)
      {
      T obj;
      return (obj.*Func)(i);
      }
      };

      void main()
      {
      CObjectT::f(7);
      auto function_t = &CObjectT::f;
      cout << typeid(function_t).name() << endl; // now it's a c-style function pointer
      }

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      Falconapollo wrote:

      The final goal is Interfacing C++ member functions with C libraries.

      If you want your C++ code to call C libraries then you use extern "C" { ...// C code and/or declarations. } If you want C libraries to call your C++ code then you must provide a C interface to it.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups