CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Location
    Bangalore, INDIA
    Posts
    20

    Conversion of C++ templates into C code

    Hi Friends,

    I am using some open source code which is in C++ language.
    But i have to convert it to C code.

    In the C++ source, there are many class templates.
    I want to convert to the templates to C code.

    I don't know how to do it.

    Please guide me.


    With Thanks and Regards,
    Arun AC

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Wink Re: Conversion of C++ templates into C code

    Quote Originally Posted by sunkingac
    In the C++ source, there are many class templates.
    I want to convert to the templates to C code.
    C does not support template classes (or classes indeed).

    Templates allow the user to re-use a piece of code with different data-types.

    The only equivalent conversion I can think of is to write methods, one-per unique data-type the template class is used with.

    So, one template class in C++ will translate to N functions in C.

    Something like this -
    Code:
    //Your C++ Code
      template <class T>
      class CTakeDifferentDataTypes
      {
      public:
      void DoSomething (T data);
      };
      
      // It is used as -
      CTakeDifferentDataTypes <int> mIntData;
      mIntData.DoSomething (20);
      
      CTakeDifferentDataTypes <float> mFloatData;
      mFloatData.DoSomething (1.0);
    This will translate to C Code as follows
    Code:
    // Handle Integers...
      void DoSomethingWithIntegers (int nData)
      {
      }
      
      // Handle Bool...
      void DoSomethingWithFloats (float fData)
      {
      }
      
      // Use these...
      DoSomethingWithIntegers (20);
     DoSomethingWithFloats (1.0);
    As you can see, C++ gives tremendous re-usabilty in the form of templates that you will lose when you port functionality to C.

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Conversion of C++ templates into C code

    There may be other things that are there in C++ that you can't do in C.

    Anyway, you really have 2 options:

    - If you are dealing with only pointers, you can use void * and lots of casting
    - If not, then probably horrible #defines and macros. So

    Code:
    typedef struct MyTemplateInC_t
    {
       int x;
       MYTEMPLATETYPE t;
    } MyTemplateInC;
    
    void doAFunction( MyTemplateInC * ptr )
    {
        // etc.
    }
    Code:
    #define MYTEMPLATETYPE struct MyStruct;
    #include "MyTemplate.h"
    // can do things with the template now
    Note that if the type will always be a struct, you can also get around the problem by forwardly declaring it, but that can lead to further problems.

    Now we'll come to the problem of wanting to have different types of the same template in yoru code. Well there's obviously only one workaround to that - we'll have to #define the type, include the file, typedef this to something, redefine the type, include the file again, typedef it to something else, etc.

    So our "template" header will obviously need the type to be defined before we can include it (and you can do a compile time check for this:
    #ifndef MYTEMPLATETYPE
    #error // etc.

    But what about the file include guard? Do we still want one? Yes - for when the type is the same, but no when the type is different. We can work around this by using the token-concatenation in macros (I think it's ## ).

    Now we have another problem though - functions in C can only have one signature, i.e. we can't overload them by having different parameter types. So what to do with our functions? Use more macro-concatenation or use void * and casting? void * and casting sounds nice but it wouldn't work here (how do we cast back?). So it looks like we need macros again.

    So I think it's worth forgetting the whole exercise at this point and enforcing the use of void* pointers and casting.

    I'll have to ask you though, what is wrong with C++ that you have to convert it to C?

  4. #4
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: Conversion of C++ templates into C code

    Cant you simulate templates somewhat with macros in C ?
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Conversion of C++ templates into C code

    Yes, lots and lots of them. Make all your functions macros. Make all your structs macros.

    So yes, maybe you can but why would you want to?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured