3

As templates need to be defined in header files so the compiler can instantiate them I find myself hiding my implemention details in a *.tcc file at the end of the "main" header. Which means the implementation details will be included by any module that includes my header.

So I would need them to be inline to avoid odr-violations. Now I know that in-class definition of class methods automatically makes them inline. Does that mean that if I define them outside of the class I still need to manually add the specifier? Also I found this IBM documentation that says I might as well specify the in-class declarations inline which would also implicitely make the definitions inline:

An equivalent way to declare an inline member function is to either declare it in the class with the inline keyword (and define the function outside of its class) or to define it outside of the class declaration using the inline keyword.

Is that correct?

/* myheader.hpp */

#include <cstdio>
#include <utility>

template <typename T>
class myclass
{
    myclass(T);

    auto print() -> void;

    T val_;
};

#inlcude "myheader.tcc"


/* myheader.tcc */

template <typename T>
/* inline? */ myclass<T>::myclass(T val)
    :   val_{ std::move(val) }
{ }

template <typename T>
/* inline? */ auto myclass<T>::print() -> void
{
    printf("There are 10 types of people in this world, those who understand binary and those who dont!\n");
}
glades
  • 3,778
  • 1
  • 12
  • 34
  • somewhat related: https://stackoverflow.com/questions/10535667/does-it-make-any-sense-to-use-inline-keyword-with-templates. unfortunately answers do not mention members of class templates – 463035818_is_not_an_ai Jan 21 '23 at 18:05
  • more related https://stackoverflow.com/q/10211273/4117728 – 463035818_is_not_an_ai Jan 21 '23 at 18:07
  • hm... second could make a good duplicate, see eg this answer https://stackoverflow.com/a/10211357/4117728 – 463035818_is_not_an_ai Jan 21 '23 at 18:08
  • yes. dupe, accepted has the relevant quote from the standard – 463035818_is_not_an_ai Jan 21 '23 at 18:09
  • @463035818_is_not_a_number But doesn't that mean that if I include a header file with templates from multiple places and I instantiate the same template (same template parameters) it will exist in two translation units at the same time and violate odr-rules? – glades Jan 21 '23 at 18:12
  • 1
    see the quote in the accepted answer. They are implicitly inline and do not violate odr – 463035818_is_not_an_ai Jan 21 '23 at 18:13
  • "There can be more than one definition [.....] provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements ..." – 463035818_is_not_an_ai Jan 21 '23 at 18:15
  • 1
    @463035818_is_not_a_number They are not implicitly `inline`, although being a templated entity comes with the same ODR effect as `inline`. Technically declaring it `inline` in one translation unit and not `inline` in another would still be IFNDR. – user17732522 Jan 21 '23 at 18:45

0 Answers0