Thread: extern "C" around template function

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    43

    extern "C" around template function

    With the following code, I get errors after compiling:
    [code]
    #ifdef __cplusplus
    extern "C"
    #endif
    template <typename VecType>
    void displayVector(const VecType& myVec);
    [\code]

    Is there a way to translate my c++ function with the template? If so, an example might be helpful.

    Thanks.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since a C compiler wouldn't understand templates, you shouldn't use them. Name different functions for all the types you instantiate the template with, and just use the types, I guess.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    43
    Quote Originally Posted by whiteflags View Post
    Since a C compiler wouldn't understand templates, you shouldn't use them. Name different functions for all the types you instantiate the template with, and just use the types, I guess.
    Thanks for the message. Well, I suppose there is a workaround. Doing the type change might be costly.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's not costly if you seriously need native C++ code to be compiled by a C compiler.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You might try function declarations of specific types or explicit template instantiations for required types. I don't know if it works, but you can try.
    Explicit template instantiation should look like

    extern "C" template foo<T>

    The 'extern "C"' is just to stop mangling for that type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very interesting puzzle of "extern char *"
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2008, 04:31 PM
  2. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  3. question on key work "extern"(C prog)
    By mycount in forum Tech Board
    Replies: 2
    Last Post: 10-18-2006, 01:58 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM