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. // 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; };

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. . // 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; };

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,

  • A clear instant definition will appear most once in the program,
  • A clear expertise will most often be defined in a program (As of 3.2) , and
  • The announcement of a clear immediate and obvious expertise will not be revealed in a program when After that declare immediately clear expertise.

    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

      template & lt; & Gt; Bull Test Class & lt; Double & gt; :: member function () {back true; };   

    must go to a .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.

    The header file should have only one non-defined declaration of that specialty

      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.  

    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 inline inline < / Code> also makes it not to be 14.7.3 / 14 in the language specification that

    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.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -