C++ - Define member function outside template-class but in header -
I have defined a simple class-template with a one-man function. It is defined outside of the classroom with an extra (obvious) specialization, which is also defined outside the classroom. In all the header files, if you include this header in many translation units, then you get a linker error due to one-definition rule. Everything is alright so far but if I put the definition of member function inside the class body, the linker error disappears and the function can be used in various translation units. . My question is why does this work? I use MSVC 2012. There are some exceptions to the templates in the ODR which I first understood the reason. But there is a difference in the definition of "base" function inside / outside of the class. 14.7 / 5 states that for a given set of given templates and template-arguments, There is no need for implementation to diagnose the violation of this rule. The second bullet applies to your case. ODR defined in 3.2 says the same thing, though in a less distilled form. Regardless of how non-specific versions of member functions are defined, specialized version definition must go to a The header file should have only one non-defined declaration of that specialty The behavior you observed in your experiments in conformity with the following: Automatically clear the explanation of that template by declaring the primary code template as has a clear explanation of the function template inline only when it is declared with inline specifier. Used or defined as deleted and independently inline with the function template. // Heading with a Template Template & lt; Class T & gt; Class test class {public: test class () {}; ~ Test class () {}; Boole member function (); }; Template & lt; Class T & gt; Bull Test Class & lt; T & gt; :: member function () {back true; } Template & lt; & Gt; Bull Test Class & lt; Double & gt; :: member function () {back true; };
// Heading with a Template Template & lt; Class T & gt; Class test class {public: test class () {}; ~ Test class () {}; Boole member function () {return true; }}; Template & lt; & Gt; Bull Test Class & lt; Double & gt; :: member function () {back true; };
template & lt; & Gt; Bull Test Class & lt; Double & gt; :: member function () {back true; };
.cpp file if the header is placed in the file, then the header output of the ODR violation after joining more than one translation unit Will do The GCC reliably detects this violation, MSVC seems less reliable in this regard but, as the quotation given above, there is no need for implementation to diagnose violations of this rule.
template & lt; & Gt; Bull Test Class & lt; Double & gt; :: member function (); The fact is that the error appears or disappears in MSVC, on the basis of the unrelated factor, as the non-specific version of the function is defined, It should be a QUEST of the MSVC compiler.
inline
inline < / Code> also makes it not to be 14.7.3 / 14 in the language specification that
Comments
Post a Comment