External Link Template? Can anyone explain this?

The name of the template has a link (3.5). A non-member function template may have an internal relationship; any other template name must have an external link. Objects created from an internally linked template are different from all objects generated in other translation units.

I know about external communication using the keyword

extern "C"

EX:

extern "C" {   template<class T>  class X { };   }

but they gave a template , should not have C links

What did the above statement really mean?

can anyone explain this?

+5
source share
4 answers

extern "C" - C. . ++- ++ , , extern "C++".

, , , , . foo a.cpp b.cpp. ++ . , , , . - , extern. extern "C".

, , . static . , const , , , extern.

, . , , . extern, , .

, .

int i; // namespace scope variable has external linkage
extern int j; // explicitly mark j with external linkage
static int k; // k has internal linkage
int const n=42; // internal linkage
extern int const m=99; // external linkage

void foo(); // foo has external linkage; it may be defined in this source file or another
extern void foo(); // explicitly mark foo with external linkage
static void bar(); // bar has internal linkage, and must be defined in this source file

void foo(){} // definition of foo, visible from other source files
void bar(){} // definition of bar, not visible from other source files (internal linkage)

static void baz(){} // declare and define baz with internal linkage

template<typename T> void foobar(){} // foobar has external linkage
template<typename T>
static void foobaz(){} // foobaz has internal linkage

void wibble()
{
    int i; // local, no linkage
    extern int i; // references i, declared above with external linkage
}

extern "C"
{
    int i2; // namespace scope variable has external linkage, and "C" linkage
    extern int j2; // explicitly mark j2 with external linkage and "C" linkage
    static int k2; // k2 has internal linkage and "C" linkage
    int const n2=42; // internal linkage and "C" linkage
    extern int const m2=99; // external linkage and "C" linkage

    void foo2(); // foo2 has external linkage and "C" linkage
    static void bar2(); // bar2 has internal linkage and "C" linkage

    void foo2(){} // definition of foo2, still with external linkage and "C" linkage
    void bar2(){} // definition of bar2, still with internal linkage and "C" linkage

    static void baz(){} // declare and define baz with internal linkage
}

--- extern "C".

extern "C", C. , , , , .

​​ extern "C". , --- C, foo foo, _foo . ++ foo, "" , $3fooV foo$void - , foo(void) foo(int) .. ++ , extern "C", C , .

extern "C" , extern "C", , , " extern "C"".

C struct s, struct . , extern "C" --- , C struct?

+23

, , , , , , . , .

, § 3.5/2, :

, , , .

, static, :

template <typename T>
static void foo( T ) {}

, , , .

: §3.5/2

, , , .

, , .

namespace {
   template <typename T>
   class test {};
}

, , , . , . static §7.3.1.1/2

static (. D);

, , :

extern "C"

. extern "C" . . extern "C" "C" C- , , dlopen family. §7.5

+7

extern "C" ++, C.

++ "" , . C .

extern "C" ++ C.

extern "C" , .

, ?

+2

, , , , , extern "C".

extern "X" X. , :

, extern "C"

false. , . . 7.5 . , (, , ) .

, , . , §14 [temp]/4:

(3.5). , , ; . , , , . , (14.7.3) C-. - C ++, . (3.2). [: - (14.5) .]

, , , , , . , , . , , , . ? ? , ? , ?

+2

All Articles