Tuesday, 13 August 2019

c++ - does unnamed namespace linkage is internal or external or a mix

I was reading a book and I have some ambiguities about linkage into namaspaces.




As far as I read,




When you are in a situation where you need to declare global symbols
as statics to avoid linkage problems, prefer to use unnamed
namespaces




Ok So inside an unnamed namespace what is defined will have internal linkage (like using static in global namespace).




Then the write added :




Template arguments cannot be names with internal linkage,
so using static variables is not possible.




Ok till now.





On the other hand, symbols in an unnamed namespace have external linkage and can be used as template arguments.




So here I am lost, is the content of an unnamed namespace will have internal or external linkage ?



#include 
template
class test {};

static int Size1 = 10;


namespace
{
int Size2 = 10;
}

int main(void)
{
test t1; // said here gonna produce a compile error but was not the case
test t2;


return 0;
}


And what about the case of normale namespace (named namespace).



Thank you.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...