template External Linkage ?can anyone Explain this?

后端 未结 4 1271
夕颜
夕颜 2021-02-10 02:05

A template name has linkage (3.5). A non-member function template can have internal linkage; any other template name shall have external linkage. Entities generat

4条回答
  •  终归单人心
    2021-02-10 02:15

    The answer to the updated question is, as I already said in the answer that applies to the original question, that you are misunderstanding what extern "C" means.

    The sequence extern "X" allows you to change the language linkage of the following function or block to language X. It does not mean external linkage, so your original premise:

    I know about external linkage using the keyword extern "C"

    is false. You don't know what it means. Refer to 7.5 in the standard. Language linkage affects how the compiler processes parameters and whether it applies (and potentially how) name mangling to the symbols.

    Taking aside your insistence in that particular error, the compiler is complaining about your code because it is invalid according to the standard. In particular §14[temp]/4:

    A template name has linkage (3.5). A non-member function template can have internal linkage; any other template name shall have external linkage. Entities generated from a template with internal linkage are distinct from all entities generated in other translation units. A template, a template explicit specialization (14.7.3), or a class template partial specialization shall not have C linkage. If the linkage of one of these is something other than C or C++, the behavior is implementation-defined. Template definitions shall obey the one definition rule (3.2). [Note: default arguments for function templates and for member functions of class templates are considered definitions for the purpose of template instantiation (14.5) and must also obey the one definition rule.]

    I really think that before trying to evaluate how different compilers comply with the standard you should take your time to understand the standard. It is quite fine to have questions, and there is people making an effort to answer them. Showing that you have read the answers and tried to grasp what they mean is just a sign of respect. What part of the last paragraph in the previous answer here is unclear? Did you read it? Did you understand it? If you didn't, why did you not ask in a comment to the answer?

提交回复
热议问题