Write a C or C ++ library with a "template"

(1). When using the C ++ template, is it correct for the compiler (for example, g ++) not to compile the template definition (which can only be in the header file, and not in the source file) directly, but to generate code based on the template definition for each of its instances, and then compile the generated code for its instances?

(2). If I want to write a C ++ library that provides template classes and template functions, it is not possible to compile the library into a common file (.so, .a), because their instances will not be anywhere in the library code, but will only be displayed in the user program? If so, does this mean that the template libraries are only source code files and not precompiled files?

How is the standard C ++ template library (STL) implemented? Is its source code precompiled or compiled with the user program?

(3). In C,

how to write a library that provides functions that act like template functions in C ++? Overloads a good solution?

If I need to write a procedure to another function for different types of arguments, is there a good way to reuse the code? Is this a good way to do this http://www.vlfeat.org/api/imop_8c_source.html ? Any other ways?

Thank you and welcome!

+3
source share
2 answers

When using the C ++ template, is it true that the compiler (e.g. g ++) will not compile the template definition.

. .

- . .

++, , (.so,.a)

, . .

++ (STL)? ?

STL .

C, , , ++? http://www.vlfeat.org/api/imop_8c_source.html? ?

( ) - .

+10

(3). C, , , ++? ?

, ? http://www.vlfeat.org/api/imop_8c_source.html? ?

, void * . , , "" (, int). 64- , stdint.h!

C, . , , , C - , C , ++.

+1

Source: https://habr.com/ru/post/1734703/


All Articles