Is it possible to define extern "C" types inside a template?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sergei

    Is it possible to define extern "C" types inside a template?

    I ran into this problem. I needed to create an entry for access to a
    library
    of functions that are "extern C", and I just can't find the right syntax, if
    it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't
    need an "extern C" linkage,
    I just need to define a type to cast a resolved function address..
    Any ideas?


    Basically , I need something like this..

    template <typename T0, typename T1, typename T2, typename T3, typename T4,
    typename T5>
    class FuncCall
    {
    FuncCall(string & aname):name(ana me){}
    private:
    extern "C" typedef T0 (*function_ptr) (T1 t1, T2 t2, T3 t3, T4 t4, T5
    t5 );
    string name;
    public:
    bool call()
    {
    function_ptr func = (function_ptr) GetProcAddress( name);
    .......
    }
    };

    Thanks,
    Sergei


  • Andrey Tarasevich

    #2
    Re: Is it possible to define extern &quot;C&quot ; types inside a template?

    Sergei wrote:
    [color=blue]
    > I ran into this problem. I needed to create an entry for access to a
    > library
    > of functions that are "extern C", and I just can't find the right syntax, if
    > it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't
    > need an "extern C" linkage,[/color]

    Unfortunately, it can't be done the way you are trying to do it. 'extern
    "C"' declarations are only allowed in namespace scope, which prevents
    anyone from creating 'extern "C"' declarations that depend on template
    parameters.
    [color=blue]
    > I just need to define a type to cast a resolved function address..
    > Any ideas?[/color]

    I can suggest a workaround. Introduce one more template parameter to you
    template and supply it with a default argument

    template <typename T0, typename T1, typename T2,
    typename T3, typename T4, typename T5,
    typename FN = T0 (*)(T1, T2, T3, T4, T5)>
    class FuncCall
    {
    typedef FN function_ptr;
    ...

    As long as you use this template with C++ functions, there's no need to
    change anything - default argument will take care of everything. But
    once you need to use it with a C function, you can do the following

    extern "C" typedef int (*CFN)(int, int, int, int, int);
    // This should be done in global namespace scope

    ...
    FuncCall<int, int, int, int, int, int, CFN> fc("some_name" ) ;

    It is not very elegant solution, since you have to do all required
    'typedef's manually, but it solves the problem.
    [color=blue]
    >
    > Basically , I need something like this..
    >
    > template <typename T0, typename T1, typename T2, typename T3, typename T4,
    > typename T5>
    > class FuncCall
    > {
    > FuncCall(string & aname):name(ana me){}
    > private:
    > extern "C" typedef T0 (*function_ptr) (T1 t1, T2 t2, T3 t3, T4 t4, T5
    > t5 );
    > string name;
    > public:
    > bool call()
    > {
    > function_ptr func = (function_ptr) GetProcAddress( name);
    > ......
    > }
    > };[/color]

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Sergei

      #3
      Re: Is it possible to define extern &quot;C&quot ; types inside a template?

      >[color=blue][color=green]
      > > I ran into this problem. I needed to create an entry for access to a
      > > library
      > > of functions that are "extern C", and I just can't find the right[/color][/color]
      syntax, if[color=blue][color=green]
      > > it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I[/color][/color]
      don't[color=blue][color=green]
      > > need an "extern C" linkage,[/color]
      >
      > Unfortunately, it can't be done the way you are trying to do it. 'extern
      > "C"' declarations are only allowed in namespace scope, which prevents
      > anyone from creating 'extern "C"' declarations that depend on template
      > parameters.[/color]

      Any particular reasons for that limitations? Or is it just another
      deficiency of ANSI C++?
      I don't see any reason for that implementation-wise. Besides, we don't have
      extern "C" declaration
      here, only a definition.
      [color=blue]
      >[color=green]
      > > I just need to define a type to cast a resolved function address..
      > > Any ideas?[/color]
      >
      > I can suggest a workaround. Introduce one more template parameter to you
      > template and supply it with a default argument
      >
      > template <typename T0, typename T1, typename T2,
      > typename T3, typename T4, typename T5,
      > typename FN = T0 (*)(T1, T2, T3, T4, T5)>
      > class FuncCall
      > {
      > typedef FN function_ptr;
      > ...
      >
      > As long as you use this template with C++ functions, there's no need to
      > change anything - default argument will take care of everything. But
      > once you need to use it with a C function, you can do the following
      >
      > extern "C" typedef int (*CFN)(int, int, int, int, int);
      > // This should be done in global namespace scope
      >
      > ...
      > FuncCall<int, int, int, int, int, int, CFN> fc("some_name" ) ;
      >
      > It is not very elegant solution, since you have to do all required
      > 'typedef's manually, but it solves the problem.
      >[color=green]
      > >
      > > Basically , I need something like this..
      > >
      > > template <typename T0, typename T1, typename T2, typename T3, typename[/color][/color]
      T4,[color=blue][color=green]
      > > typename T5>
      > > class FuncCall
      > > {
      > > FuncCall(string & aname):name(ana me){}
      > > private:
      > > extern "C" typedef T0 (*function_ptr) (T1 t1, T2 t2, T3 t3, T4 t4,[/color][/color]
      T5[color=blue][color=green]
      > > t5 );
      > > string name;
      > > public:
      > > bool call()
      > > {
      > > function_ptr func = (function_ptr) GetProcAddress( name);
      > > ......
      > > }
      > > };[/color]
      >[/color]

      Thanks, Andrey,

      that's exactly what I did. And that workaround is working. But, as you said,
      it is not elegant ( why use an extra
      parameter type if it doesn' bring any new information into template ). I
      really hoped that I could get away
      without typedef-ing every function that I will have to implement. No big
      deal though.

      Sergei


      Comment

      • Victor Bazarov

        #4
        Re: Is it possible to define extern &quot;C&quot ; types inside a template?

        Sergei wrote:[color=blue][color=green][color=darkred]
        >>>I ran into this problem. I needed to create an entry for access to a
        >>>library
        >>>of functions that are "extern C", and I just can't find the right[/color][/color]
        >
        > syntax, if
        >[color=green][color=darkred]
        >>>it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I[/color][/color]
        >
        > don't
        >[color=green][color=darkred]
        >>>need an "extern C" linkage,[/color]
        >>
        >>Unfortunately , it can't be done the way you are trying to do it. 'extern
        >>"C"' declarations are only allowed in namespace scope, which prevents
        >>anyone from creating 'extern "C"' declarations that depend on template
        >>parameters.[/color]
        >
        >
        > Any particular reasons for that limitations? Or is it just another
        > deficiency of ANSI C++?
        > I don't see any reason for that implementation-wise. Besides, we don't have
        > extern "C" declaration
        > here, only a definition.[/color]

        Just a reminder, every definition is also a declaration, so we _do_ have a
        declaration here.

        V

        Comment

        • Denis Remezov

          #5
          Re: Is it possible to define extern &quot;C&quot ; types inside a template?

          Sergei wrote:[color=blue]
          >[color=green]
          > >[color=darkred]
          > > > I ran into this problem. I needed to create an entry for access to a
          > > > library
          > > > of functions that are "extern C", and I just can't find the right[/color][/color]
          > syntax, if[color=green][color=darkred]
          > > > it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I[/color][/color]
          > don't[color=green][color=darkred]
          > > > need an "extern C" linkage,[/color]
          > >
          > > Unfortunately, it can't be done the way you are trying to do it. 'extern
          > > "C"' declarations are only allowed in namespace scope, which prevents
          > > anyone from creating 'extern "C"' declarations that depend on template
          > > parameters.[/color]
          >
          > Any particular reasons for that limitations? Or is it just another
          > deficiency of ANSI C++?
          > I don't see any reason for that implementation-wise.[/color]

          [snip]

          You cannot have two or more extern "C" functions with the same name in one
          program, which makes sense, because there is no function name overloading in C.

          Denis

          Comment

          Working...